【发布时间】:2015-12-19 08:28:23
【问题描述】:
如何在应用标题旁边有一个后退按钮?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.create_back, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch(item.getItemId()) {
case R.id.back: // back to previous activity
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
后退按钮显示在右侧。 如何将其移到左侧?
重新创建
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:icon="@mipmap/back_to"
android:id="@+id/back"
android:orderInCategory="100"
android:title="Back"
app:showAsAction="always" />
</menu>
我可以创建一个后退图标,只是不知道如何将它移到左侧 一边。
【问题讨论】:
-
你的 create_back 布局文件是什么样的?
-
您不需要创建自己的箭头项目。使用阿米特的答案。
R.id.home是您想要在左侧的箭头,它默认包含在 Android 中。它被称为导航:developer.android.com/training/implementing-navigation/… -
@MC10 我必须删除 create_menu 吗?
-
只需从 create_back.xml 文件中删除
back项即可。 -
@MC10 不太明白@@...删除该项目,让 create_back 为空?
标签: android android-layout android-studio android-actionbar back