Android:ActionBar相关

来源:互联网 发布:Java 动态取得属性值 编辑:程序博客网 时间:2024/06/05 16:39

官方介绍ActionBar的地址:http://developer.android.com/guide/topics/ui/actionbar.html

                                     (图一)

1:一个app icon

2:两个action items

3:action overflow


如果当前App使用API Level11或更高,则引用

android.app.ActionBar

如果当前App适配最低版本为API Level7及以上则使用

android.support.v7.app.ActionBar


具体使用方法如下:
1.Adding the Action Bar

    1.当最低版本为API Level 7或更高时:

        a.Activity继承ActionBarActivity

        b.AndroidManifest.xml中配置App主题为:android:theme="@style/Theme.AppCompat"

        或者使用其他继承于Theme.AppCompat的主题.

    2.当最低版本为API level 11或更高时

        a.使用Theme.Holo(或继承于 Theme.Holo)主题,则所有的activity就包含了ActionBar

2.Adding Action Items

    在Menu文件中编辑Action Items,例子如下

    注意(xmlns:app),当你使用support library时,showAsAction属性是属于support library而不是Androidframework

    所以必须自定义命名空间作为前缀给属于support library的属性下定义.(命名空间app可修改)

    推荐使用ifRoom属性

<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.actionbarexample.MainActivity" >    <item        android:id="@+id/action_search"        android:icon="@drawable/search"        app:showAsAction="ifRoom"        android:title="@string/action_search"/>    <item        android:id="@+id/action_settings"        android:orderInCategory="100"        android:title="@string/action_settings"        app:showAsAction="never"/></menu>

3.Handling clicks on action items

    系统调用以下方法处理ActionBar中Item的点击事件

/** * ActionBar Item OnClick */@Overridepublic 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.int id = item.getItemId();if (id == R.id.action_settings) {Toast.makeText(this, "This is action_settings", Toast.LENGTH_SHORT).show();} else if (id == R.id.action_search) {Toast.makeText(this, "This is action_search", Toast.LENGTH_SHORT).show();}return super.onOptionsItemSelected(item);}

4.Using split action bar

盗一张官网上的图用用

    对应低分辨率小屏幕手机时可以在文件AndroidManifest.xml使用如下属性来达到上图Bottom的效果

    API Level 7或更高需要在文件中做如下配置(meta-data)

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.actionbarexample"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="10"        android:targetSdkVersion="15" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/Theme.AppCompat.Light" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <meta-data                android:name="android.support.UI_OPTIONS"                android:value="splitActionBarWhenNarrow" />            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>
   API Level 14或更高需要在文件中做如下配置(android:uiOptions="splitActionBarWhenNarrow")

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.actionbarexample"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="14"        android:targetSdkVersion="15" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/Theme.AppCompat.Light"        android:uiOptions="splitActionBarWhenNarrow">        <activity            android:name=".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>    </application></manifest>






0 0
原创粉丝点击