android开发training之actionbar之2

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无论是否溢出,总会显示。
    withTextwithText值示意Action bar要显示文本标题。Action bar会尽可能的显示这个
标题,但是,如果图标有效并且受到Action bar空间的限制,文本标题有可
能显示不全。
   collapseActionView  声明了这个操作视窗应该被折叠到一个按钮中,当用户选择这个按钮时,这个操作视窗展开。否则,
这个操作视窗在默认的情况下是可见的,并且即便在用于不适用的时候,也要占据操作栏的有效空间。
一般要配合ifRoom一起使用才会有效果。

2.为action bar添加动作:

@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.

API16以下,需要在   <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使能。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值