Android UI ActionBar功能-Action Bar 左上角的向上或返回按钮
ActionBar在左上角还提供了一个向上或返回的按钮,默认情况下是隐藏的需要在代码中开启:
官方文档:http://wear.techbrood.com/training/basics/actionbar/adding-buttons.html#UpNav
向上功能:
先打开功能选项,设置ActionBar的DisplayHomeAsUpEnabled=true:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getActionBar().setDisplayHomeAsUpEnabled(true); }添加一个Activity为ParentActivity 同时新建ParentActivity的布局文件:
public class ParentActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.parent_activity); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="parent activity"> </TextView> </LinearLayout>
然后在AndroidManifest.xml文件中配置向上功能:
详细请查看上方的官方说明文档;
返回功能:
先打开功能选项,设置ActionBar的DisplayHomeAsUpEnabled=true:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getActionBar().setDisplayHomeAsUpEnabled(true); }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); break; default: break; } return super.onOptionsItemSelected(item); }