Android 学习笔记四:创建工具栏按钮
原文:http://blog.csdn.net/lihongxun945/article/details/48951199
前面我們已經(jīng)可以在一個(gè)Activity中添加一些按鈕之類的組件。由于手機(jī)的屏幕很小,所以很多時(shí)候我們會(huì)需要用到工具欄,通過(guò)下拉菜單之類的方式來(lái)節(jié)省空間。
Android 提供了對(duì)工具欄按鈕的強(qiáng)大支持。
增加一個(gè)工具欄按鈕
我們現(xiàn)在給?MainActivity?增加一個(gè)搜索按鈕。增加一個(gè)按鈕需要做這三件事
一,在?res/menu/activity_main.xml?中增加一個(gè)按鈕的配置。在自動(dòng)生成的項(xiàng)目中已經(jīng)有了這個(gè)文件,并且自動(dòng)創(chuàng)建了一個(gè)設(shè)置按鈕,我們只要增加一個(gè)搜索按鈕即可
<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=".MainActivity"><item android:id="@+id/action_search"android:icon="@drawable/ic_search_white_24dp"android:title="@string/action_search"android:showAsAction="ifRoom" /><!-- Settings, should always be in the overflow --><item android:id="@+id/action_settings"android:title="@string/action_settings"android:showAsAction="never" /> </menu>注意其中我們用到了?drawable?和?string?中的兩個(gè)資源,需要自己去創(chuàng)建一下。
二,在?MainActivity?中引用配置好的按鈕
在?MainActivity?中已經(jīng)聲明了一個(gè)?onCreateOptionsMenu?方法了,其實(shí)根本不用改:
@Override public boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.menu_main, menu);return true; }三,給按鈕綁定事件
這里我們也已經(jīng)有一個(gè)?onOptionsItemSelected?方法了,只需要在其中增加一下事件監(jiān)聽就行了
@Override public 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();//noinspection SimplifiableIfStatementswitch (id) {case R.id.action_search: Toast.makeText(this, "你點(diǎn)擊了搜索", Toast.LENGTH_SHORT).show();case R.id.action_settings: Toast.makeText(this, "你點(diǎn)擊了設(shè)置", Toast.LENGTH_SHORT).show();}return super.onOptionsItemSelected(item); }增加返回按鈕
很多時(shí)候,我們需要給不是?MainActivity?的活動(dòng)都增加一個(gè)返回按鈕,以方便用戶通過(guò)返回按鈕能返回到上一個(gè)活動(dòng)。安卓提供了返回按鈕的默認(rèn)支持,我們只需要配置一下即可,并不用寫返回的邏輯代碼。
只需要兩步既可以實(shí)現(xiàn)返回按鈕的功能。這里我們希望能在?ProfileActivity?中增加一個(gè) 返回按鈕,點(diǎn)擊返回MainActivity?中
一,在?AndroidManifest.xml?中 Activity 的聲明中加上父活動(dòng)的聲明:
<activityandroid:name=".ProfileActivity"android:label="@string/title_activity_profile"android:parentActivityName=".MainActivity"><!-- Parent activity meta-data to support 4.0 and lower --><meta-dataandroid:name="android.support.PARENT_ACTIVITY"android:value=".MainActivity" /><intent-filter><action android:name="com.lihongxun.Profile" /><category android:name="android.intent.category.DEFAULT" /></intent-filter></activity>注意,如果想支持 4.0 及以下設(shè)備需要加上?meta-data?那一行,不然就只需要加上android:parentActivityName=".MainActivity”?即可
二,在?ProfileActivity?中啟用返回按鈕。在?onCreate?函數(shù)中加上一行代碼即可:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);然后再運(yùn)行試試。
三 覆蓋式工具欄
默認(rèn)情況下,頂部的工具欄是占用空間的,他會(huì)把內(nèi)容區(qū)擠到下面去。有時(shí)候你可能會(huì)希望它是覆蓋在內(nèi)容區(qū)上的,特別是你希望能動(dòng)態(tài)顯示隱藏工具欄的時(shí)候。?
只需要在?styles.xml?中聲明使用覆蓋式工具欄即可
中間的兩行?item?就是聲明覆蓋式工具欄,注意第二行是兼容庫(kù)的時(shí)候用。
轉(zhuǎn)載于:https://www.cnblogs.com/mochaMM/p/5131462.html
總結(jié)
以上是生活随笔為你收集整理的Android 学习笔记四:创建工具栏按钮的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: JDK_下载网址
- 下一篇: ExtJs6 理解 -- Ext.dat