actiong bar是一個方便快捷的導航神器。它可以標識活動的標題,突出活動的一些關鍵操作(如“搜索”、“創建”、“共享”等)。
1.在一個項目中增加action bar
在xml中定義action bar的每個因子的屬性,如下:
main_activity_actions。xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.demo.activity.DisplayMessageActivity">
<item
android:id="@+id/action_setting1"
android:title="setting1"
app:showAsAction="never" />
<item
android:id="@+id/action_setting2"
android:title="setting2"
app:showAsAction="never" />
<item
android:id="@+id/action_search"
android:title="search"
app:showAsAction="always" />
</menu>
上面的action bar中定義了三個item,每個item都有id,title等屬性,
ifRoom | 會顯示在Item中,但是如果已經有4個或者4個以上的Item時會隱藏在溢出列表中。當然個 數並不僅僅侷限於4個,依據屏幕的寬窄而定 |
never | 永遠不會顯示。只會在溢出列表中顯示,而且只顯示標題,所以在定義item的時候,最好 把標題都帶上。 |
always | 無論是否溢出,總會顯示。 |
withText | withText值示意Action bar要顯示文本標題。Action bar會儘可能的顯示這個 標題,但是,如果圖標有效並且受到Action bar空間的限制,文本標題有可 能顯示不全。 |
collapseActionView | 聲明瞭這個操作視窗應該被摺疊到一個按鈕中,當用戶選擇這個按鈕時,這個操作視窗展開。否則, 這個操作視窗在默認的情況下是可見的,並且即便在用於不適用的時候,也要佔據操作欄的有效空間。 一般要配合ifRoom一起使用纔會有效果。 |
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.main_activity_actions,menu);
return super.onCreateOptionsMenu(menu);
}
這樣,在點擊action bar的擴展按鈕後,就會彈出item。
3.相應action bar中的動作
就是重寫onOptionsItemSelected這個類
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_setting1:
return true;
case R.id.action_setting2:
return true;
case R.id.action_search:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
在case中添加要做的事情。
4.爲low-level activities添加向上的按鈕(Up button)
When running on Android 4.1 (API level 16) or higher, or when using ActionBarActivity
from
the Support Library, performing Upnavigation
simply requires that you declare the parent activity in the manifest file and enable the Up button
for the action bar.
API 16以上,只需要在清單列表文件(manifest file)中聲明一個activity的parent activity,並將Up button使能。
Beginning in Android 4.1 (API level 16), you can declare the logical parent of each activity by specifying the android:parentActivityName
attribute
in the <activity>
element.
API16以上,只需要在清單列表文件中的<activity>
因子中指定子activity的父activity,用android:parentActivityName
If your app supports Android 4.0 and lower, include the Support Library with your app and add a <meta-data>
element
inside the <activity>
. Then specify the parent activity
as the value forandroid.support.PARENT_ACTIVITY
, matching the android:parentActivityName
attribute.
<activity>的
<meta-data>因子裏面用下面這種方法指定
<application ... > ... <!-- The main/home activity (it has no parent activity) --> <activity android:name="com.example.myfirstapp.MainActivity" ...> ... </activity> <!-- A child of the main activity --> <activity android:name="com.example.myfirstapp.DisplayMessageActivity" android:label="@string/title_activity_display_message" android:parentActivityName="com.example.myfirstapp.MainActivity" > <!-- Parent activity meta-data to support 4.0 and lower --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.myfirstapp.MainActivity" /> </activity> </application>
本demo是API16起步的,所以用下面的方法指定
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".activity.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.DisplayMessageActivity"
android:parentActivityName=".activity.MainActivity"
android:label="@string/app_name">
</activity>
</application>
</manifest>
activity_main.xml中添加一個textview用於標識當前頁,一個button用於跳轉到子activity。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.demo.activity.MainActivity">
<TextView
android:text="this is parent view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/goToChildBtn"
android:text="go to child view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
activity_displaymessage.xml是子activity的佈局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.demo.activity.DisplayMessageActivity">
<TextView
android:text="this is child view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
一個textview用於標識。
mainactivity中關聯button並響應點擊事件,跳轉到子activity。
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.goToChildBtn:
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
startActivity(intent);
break;
}
}
DisplayMessageActivity中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaymessage);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
getActionBar().setDisplayHomeAsUpEnabled(true);
返回上一個activity使能。