actionbar

来源:互联网 发布:linux vi 序号 编辑:程序博客网 时间:2024/06/07 18:47

Figure 1. An action bar that includes the [1] app icon, [2] two action items, and [3] action overflow.

actionbar提供了一些功能:

  • 提供一个专门的空间给你的应用身份和指示应用程序用户的位置。
  • 突出和显示重要一些的操作(such as Search).
  • 在应用程序中支持统一的导航和视图切换(标签或下拉列表).
ActionBar API第一次增加了Android 3.0(API级别11),但他们也可以在支持库兼容安卓2.1(API 7级)及以上。

注意:首先确定导入所在包下的类和api有关系:1、android3.0以上(api11以上)import android.app.ActionBar  2、支持3.0以下,就是 2.1以上(api 7以上)  import android.support.v7.app.ActionBar

首先要支持appcompat v7 

  1. 创建activity 并且继承 ActionBarActivity.
  2. Use (or extend) one of the Theme.AppCompat themes for your activity. For example:
    <activity android:theme="@style/Theme.AppCompat.Light" ... >

tip:api 11以上

action bar包含所有activities都使用主题Theme.Holo  (或者他们的子类), which is the default theme when either the targetSdkVersion or minSdkVersion attribute is set to "11" or higher. If you don't want the action bar for an activity, set the activity theme to Theme.Holo.NoActionBar.

Removing the action bar

ActionBar actionBar = getSupportActionBar();actionBar.hide();

On API level 11 or higher

获取 ActionBar 是通过 getActionBar() 方法

当action bar隐藏了,系统会调整布局填充剩余的屏幕空间,全屏显示.。如果要继续显示就调用 show()..

小心隐藏和删除actionbar使您activity布局以占actionbar所占用的空间。如果要经常隐藏和显示actionbar,最好用overlay mode ,这个模式可以解决占用空间问题,使用这个模式的方式是自定义主题,设置windowActionBarOverlay 的值是true
添加  Action Items

 the system populates the action items by calling your activity'sonCreateOptionsMenu() method. Use this method to inflate a menu resource that defines all the action items. For example, here's a menu resource defining a couple of menu items:

res/menu/main_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:id="@+id/action_search"          android:icon="@drawable/ic_action_search"          android:title="@string/action_search"/>    <item android:id="@+id/action_compose"          android:icon="@drawable/ic_action_compose"          android:title="@string/action_compose" /></menu>

Then in your activity's onCreateOptionsMenu() method, inflate the menu resource into the given Menu to add each item to the action bar:

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu items for use in the action bar    MenuInflater inflater = getMenuInflater();    inflater.inflate(R.menu.main_activity_actions, menu);    return super.onCreateOptionsMenu(menu);}

To request that an item appear directly in the action bar as an action button, include showAsAction="ifRoom" in the <item> tag. For example:

<menu xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >    <item android:id="@+id/action_search"          android:icon="@drawable/ic_action_search"          android:title="@string/action_search"          yourapp:showAsAction="ifRoom"  />    ...</menu>

If your menu item supplies both a title and an icon—with the title and icon attributes—then the action item shows only the icon by default. If you want to display the text title, add "withText" to the showAsActionattribute. For example:

<item yourapp:showAsAction="ifRoom|withText" ... />

Handling clicks on action items

When the user presses an action, the system calls your activity's onOptionsItemSelected() method. Using theMenuItem passed to this method, you can identify the action by calling getItemId(). This returns the unique ID provided by the <item> tag's id attribute so you can perform the appropriate action. For example:

@Overridepublic boolean onOptionsItemSelected(MenuItem item) {    // Handle presses on the action bar items    switch (item.getItemId()) {        case R.id.action_search:            openSearch();            return true;        case R.id.action_compose:            composeMessage();            return true;        default:            return super.onOptionsItemSelected(item);    }}

Using split action bar

Split action bar provides a separate bar at the bottom of the screen to display all action items when the activity is running on a narrow screen (such as a portrait-oriented handset).

  1. Add uiOptions="splitActionBarWhenNarrow" to each <activity> element or to the <application> element. This attribute is understood only by API level 14 and higher (it is ignored by older versions).
  2. To support older versions, add a <meta-data> element as a child of each <activity> element that declares the same value for "android.support.UI_OPTIONS".

For example:

<manifest ...>    <activity uiOptions="splitActionBarWhenNarrow" ... >        <meta-data android:name="android.support.UI_OPTIONS"                   android:value="splitActionBarWhenNarrow" />    </activity></manifest>

Using split action bar also allows navigation tabs to collapse into the main action bar if you remove the icon and title (as shown on the right in figure 3). To create this effect, disable the action bar icon and title withsetDisplayShowHomeEnabled(false) and setDisplayShowTitleEnabled(false).

To enable the app icon as an Up button, call setDisplayHomeAsUpEnabled(). For example:

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_details);    ActionBar actionBar = getSupportActionBar();    actionBar.setDisplayHomeAsUpEnabled(true);    ...}
Specify the parent activity in the manifest file.

This is the best option when the parent activity is always the same. By declaring in the manifest which activity is the parent, the action bar automatically performs the correct action when the user presses the Upbutton.

Beginning in Android 4.1 (API level 16), you can declare the parent with the parentActivityName attribute in the <activity> element.

To support older devices with the support library, also include a <meta-data> element that specifies the parent activity as the value for android.support.PARENT_ACTIVITY. For example:

<application ... >    ...    <!-- The main/home activity (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 API level 7+ -->        <meta-data            android:name="android.support.PARENT_ACTIVITY"            android:value="com.example.myfirstapp.MainActivity" />    </activity></application>

Once the parent activity is specified in the manifest like this and you enable the Up button withsetDisplayHomeAsUpEnabled(), your work is done and the action bar properly navigates up.

后面下次再看,看英文,看着真累
0 0
原创粉丝点击