国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

ToolBar介紹及使用

2019-11-09 16:00:42
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

ToolBar介紹


APP Bar1,應(yīng)用欄或者操作欄,使用應(yīng)用欄可讓您的應(yīng)用與其他 Android 應(yīng)用保持一致,允許用戶快速了解如何使用您的應(yīng)用并獲得一流的體驗(yàn)。應(yīng)用欄的主要功能包括:

一個(gè)專用區(qū)域,可以標(biāo)識(shí)您的應(yīng)用并指示用戶在應(yīng)用中的位置;以可預(yù)測(cè)的方式訪問(wèn)搜索等重要操作;支持導(dǎo)航和視圖切換(通過(guò)標(biāo)簽頁(yè)或下拉列表);

Beginning with Android 3.0 (API level 11), all activities that use the default theme have an ActionBar as an app bar. However, app bar features have gradually been added to the native ActionBar over various Android releases. As a result, the native ActionBar behaves differently depending on what version of the Android system a device may be using. By contrast, the most recent features are added to the support library’s version of Toolbar, and they are available on any device that can use the support library.

For this reason, you should use the support library’s Toolbar class to implement your activities’ app bars. Using the support library’s toolbar helps ensure that your app will have consistent behavior across the widest range of devices. For example, the Toolbar widget PRovides a material design experience on devices running Android 2.1 (API level 7) or later, but the native action bar doesn’t support material design unless the device is running Android 5.0 (API level 21) or later.

從 Android 3.0(API 級(jí)別 11)開(kāi)始,所有使用默認(rèn)主題的 Activity 均使用 ActionBar 作為應(yīng)用欄。不過(guò),經(jīng)過(guò)不同 Android 版本的演化,應(yīng)用欄功能已逐漸添加到原生 ActionBar 中。因此,原生 ActionBar 的行為會(huì)隨設(shè)備使用的 Android 系統(tǒng)的版本而發(fā)生變化。相比之下,最新功能已添加到支持庫(kù)版本的 Toolbar 中,并且這些功能可以在任何能夠使用該支持庫(kù)的設(shè)備上使用。

因此,您應(yīng)使用支持庫(kù)的 Toolbar 類來(lái)實(shí)現(xiàn) Activity 的應(yīng)用欄。使用支持庫(kù)的工具欄有助于確保您的應(yīng)用在最大范圍的設(shè)備上保持一致的行為。例如,Toolbar 小部件能夠在運(yùn)行 Android 2.1(API 級(jí)別 7)或更高版本的設(shè)備上提供 Material Design 體驗(yàn),但除非設(shè)備運(yùn)行的是 Android 5.0(API 級(jí)別 21)或更高版本,否則原生操作欄不會(huì)支持 Material Design。

所以,官方建議使用支持庫(kù)的Toolbar來(lái)實(shí)現(xiàn)所有Activity的運(yùn)用欄。

向 Activity 添加工具欄

以下步驟說(shuō)明了如何將 Toolbar 設(shè)置為 Activity 的應(yīng)用欄:

1.引入 v7 appcompat 支持庫(kù);

2.使Activity繼承 AppCompatActivity,如;

public class MyActivity extends AppCompatActivity { // ...}

3.添加appcompat 的其中一個(gè) NoActionBar 主題

在應(yīng)用清單中,將 <application> 元素設(shè)置為使用 appcompat 的其中一個(gè) NoActionBar 主題,或者針對(duì)某個(gè)Activity單獨(dú)設(shè)置主題。使用這些主題中的一個(gè)可以防止應(yīng)用使用原生 ActionBar 類提供應(yīng)用欄。例如:

<application android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

或者為某個(gè)Activity設(shè)置主題:

<activity android:name="...MyActivity" ... android:theme="@style/A4.在 Activity 的布局添加一個(gè) Toolbar:<android.support.v7.widget.Toolbar android:id="@+id/my_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary"/>

5.在Activity中引用Toolbar:

在 Activity 的 onCreate() 方法中,調(diào)用 Activity 的 setSupportActionBar() 方法,然后傳遞 Activity 的工具欄。該方法會(huì)將工具欄設(shè)置為 Activity 的應(yīng)用欄。例如:

@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); setSupportActionBar(myToolbar);}

添加和處理工具欄操作

應(yīng)用欄空間很有限,如果一個(gè)應(yīng)用程序定義了過(guò)多按鈕,應(yīng)用欄將多余的按鈕放在溢出菜單。運(yùn)用欄還可以指定一個(gè)按鈕應(yīng)該始終顯示在溢出菜單還是顯示在應(yīng)用欄中。

添加操作按鈕

運(yùn)用欄上所有的按鈕都是定義在 res/menu/ 目錄下的 menu resource文件中,所以,添加操作按鈕即是添加一個(gè)menu文件,并在Activity中onCreateOptionsMenu 指定menu文件。

定義運(yùn)用欄按鈕:

<menu
xmlns:android="http://schemas.android.com/apk/res/android"> <!-- should appear as action button if possible --> <item android:id="@+id/action_favorite" android:icon="@drawable/ic_favorite_black_48dp" android:title="@string/action_favorite" app:showAsAction="ifRoom"/> <!--should always be in the overflow --> <item android:id="@+id/action_settings" android:title="@string/action_settings" app:showAsAction="never"/></menu>

在Activity中onCreateOptionsMenu 指定menu文件:

@Overridepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu getMenuInflater().inflate(R.menu.main, menu); return true;}

響應(yīng)操作事件

當(dāng)用戶選擇一個(gè)應(yīng)用欄目時(shí),系統(tǒng)調(diào)用Activity中的onoptionsitemselected()回調(diào)方法,并通過(guò)一個(gè)MenuItem對(duì)象表示這項(xiàng)被點(diǎn)擊。

@Overridepublic boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_settings: ... return true; case R.id.action_favorite: ... return true; default: // Invoke the superclass to handle it. return super.onOptionsItemSelected(item); }}

參考


APPBar:https://developer.android.com/training/appbar/setting-up.html ?
上一篇:AS混淆代碼

下一篇:MVP模式

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 教育| 峡江县| 房产| 老河口市| 茂名市| 永胜县| 南通市| 博湖县| 海南省| 临洮县| 九龙城区| 新丰县| 永城市| 新密市| 娱乐| 平顺县| 新干县| 于都县| 北辰区| 龙胜| 图木舒克市| 肥城市| 隆德县| 永善县| 文成县| 嫩江县| 宝丰县| 高要市| 万安县| 嵊泗县| 霍山县| 眉山市| 洛阳市| 洪雅县| 霍邱县| 望城县| 丰镇市| 金湖县| 巴南区| 曲松县| 莱州市|