我正在尝试在Android的动作栏上做一些事情。
我已经在操作栏的右侧添加了新项目。
如何更改操作栏的左侧?我想改变图标和文本,我想在其他屏幕的操作栏中添加一个“返回按钮”
我正在尝试在Android的动作栏上做一些事情。
我已经在操作栏的右侧添加了新项目。
如何更改操作栏的左侧?我想改变图标和文本,我想在其他屏幕的操作栏中添加一个“返回按钮”
当前回答
我得到了非静态方法setTitle(CharSequence)不能从静态上下文错误中引用,因为我在静态PlaceholderFragment类中使用了setTitle()。我通过使用getActivity(). getactionbar()来解决它。setTitle(“新标题”);
其他回答
转到您想要更改操作栏标题名称并写入的特定活动的清单 android: label = "标题名称"
我得到了非静态方法setTitle(CharSequence)不能从静态上下文错误中引用,因为我在静态PlaceholderFragment类中使用了setTitle()。我通过使用getActivity(). getactionbar()来解决它。setTitle(“新标题”);
在Android 5.0中,材料设计指南不鼓励在actionBar中使用图标
要启用它,请添加以下代码
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
功劳归于这篇文章的作者
这对我来说很有用:
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeAsUpIndicator(R.mipmap.baseline_dehaze_white_24);
为此,可以采用两种方式:XML或Java。看这里:如何更改操作栏上的文本
So:
XML:
<activity android:name=".Hello_World"
android:label="This is the Hello World Application">
</activity>
Java:
public class TitleBar extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if ( customTitleSupported ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
}
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if ( myTitleText != null ) {
myTitleText.setText("NEW TITLE");
// user can also set color using "Color" and then "Color value constant"
// myTitleText.setBackgroundColor(Color.GREEN);
}
}
}