我正在尝试在Android的动作栏上做一些事情。
我已经在操作栏的右侧添加了新项目。
如何更改操作栏的左侧?我想改变图标和文本,我想在其他屏幕的操作栏中添加一个“返回按钮”
我正在尝试在Android的动作栏上做一些事情。
我已经在操作栏的右侧添加了新项目。
如何更改操作栏的左侧?我想改变图标和文本,我想在其他屏幕的操作栏中添加一个“返回按钮”
当前回答
这对我来说很有用:
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeAsUpIndicator(R.mipmap.baseline_dehaze_white_24);
其他回答
在活动中的onCreate函数中添加以下代码。
setTitle("NewName");
我在onNavigationItemSelected内部使用了以下调用:
HomeActivity.this.setTitle(item.getTitle());
为此,可以采用两种方式: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);
}
}
}
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(getString(R.string.titolo));
actionBar.setIcon(R.mipmap.ic_launcher);
actionBar.setDisplayShowHomeEnabled(true);
你可以在你的可绘制文件夹中添加任何你想要的图标,然后在你的AndroidManifest.xml文件中更改这一行:
android:icon="@drawable/ic_launcher"
来匹配你的图标的名字。或者把你的图标作为ic_launcher,如果它们是相同的图标。至于它说什么,添加或改变任何字符串匹配的res/values/strings.xml文件。然后,再次在AndroidManifest.xml文件中修改这一行:
android:label="@string/app_name"
到他们的字符串中。您必须为整个应用程序以及您想要的任何活动执行此操作,但行是相同的。
希望这能有所帮助。