溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

android中 ToolBar如何使用

發(fā)布時(shí)間:2021-06-28 17:37:59 來(lái)源:億速云 閱讀:263 作者:Leah 欄目:移動(dòng)開(kāi)發(fā)

android中 ToolBar如何使用,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

使用ToolBar必須在Activity配置theme中去掉ActionBar,例如使用

Theme.AppCompat.Light.NoActionBar

或者在主題style中自定義style:

<style name="AppThemeNoColour" parent="Theme.AppCompat.Light.NoActionBar"> 
        <!--下面兩行是取消ActionBar和去掉title;這兩行必須要,下面的四行可以自定義去留--> 
        <item name="android:windowActionBar">false</item> 
        <item name="android:windowNoTitle">true</item> 
        <!-- 狀態(tài)欄顏色 --> 
        <item name="colorPrimaryDark">@android:color/black</item> 
        <!-- 窗口的背景顏色 --> 
        <item name="android:windowBackground">@android:color/white</item> 
        <item name="colorAccent">@color/colorAccent</item> 
        <item name="android:windowDrawsSystemBarBackgrounds">false</item> 
    </style>

接下來(lái)我們看下布局:

<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" 
  tools:context="www.toolbar.com.toolbardemo.MainActivity" 
  android:orientation="vertical"> 
 
  <android.support.v7.widget.Toolbar 
    android:layout_width="match_parent" 
    android:layout_height="56dp" 
    android:id="@+id/toolbar" 
    android:background="@android:color/holo_blue_dark" 
    /> 
 
</LinearLayout>

ToolBar可以設(shè)置返回鍵、圖標(biāo)、標(biāo)題、副標(biāo)題、菜單

Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar); 
 
//把布局中的Toolbar當(dāng)作ActionBar 
setSupportActionBar(toolbar); 
//設(shè)置圖標(biāo) 
toolbar.setLogo(R.mipmap.ic_launcher); 
//設(shè)置標(biāo)題 
getSupportActionBar().setTitle("ZhengDang"); 
//設(shè)置副標(biāo)題 
toolbar.setSubtitle("2015.01.13"); 
//設(shè)置返回鍵 
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

返回鍵還可以設(shè)置監(jiān)聽(tīng):

toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        Toast.makeText(MainActivity.this,"你點(diǎn)擊了返回按鈕",Toast.LENGTH_SHORT).show(); 
 
      } 
    });

配置菜單首先要先自定義menu布局:在res ---> menu 下創(chuàng)建xml文件:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:app="http://schemas.android.com/apk/res-auto"> 
 
  <!-- 
    showAsAction: 
    ifRoom 
    會(huì)顯示在Item中,但是如果已經(jīng)有4個(gè)或者4個(gè)以上的Item時(shí)會(huì)隱藏在溢出列表中。 
    當(dāng)然個(gè)數(shù)并不僅僅局限于4個(gè),依據(jù)屏幕的寬窄而定 
 
    never 
    永遠(yuǎn)不會(huì)顯示。只會(huì)在溢出列表中顯示,而且只顯示標(biāo)題,所以在定義item的時(shí)候,最好把標(biāo)題都帶上。 
 
    always 
    無(wú)論是否溢出,總會(huì)顯示。 
 
    withText 
    withText值示意Action bar要顯示文本標(biāo)題。Action bar會(huì)盡可能的顯示這個(gè)標(biāo)題, 
    但是,如果圖標(biāo)有效并且受到Action bar空間的限制,文本標(biāo)題有可能顯示不全。 
 
    collapseActionView 
    聲明了這個(gè)操作視窗應(yīng)該被折疊到一個(gè)按鈕中,當(dāng)用戶(hù)選擇這個(gè)按鈕時(shí), 
    這個(gè)操作視窗展開(kāi)。否則,這個(gè)操作視窗在默認(rèn)的情況下是可見(jiàn)的,并且即便在用于不適用的時(shí)候, 
    也要占據(jù)操作欄的有效空間。 
    --> 
  <item 
    android:id="@+id/action_setting" 
    android:orderInCategory="100" 
    android:title="settings" 
    app:showAsAction="always" 
    android:icon="@drawable/scan_bg" 
    /> 
 
  <item 
    android:id="@+id/action_ufc" 
    android:orderInCategory="100" 
    android:title="UFC" 
    /> 
 
  <item 
    android:id="@+id/action_wlf" 
    android:orderInCategory="100" 
    android:title="武林風(fēng)" 
    /> 
  <item 
    android:id="@+id/action_klf" 
    android:orderInCategory="100" 
    android:title="昆侖決" 
    /> 
</menu>

 然后在MainActivity眾設(shè)置菜單按鈕:

/** 
   * 設(shè)置菜單第一步: 
   * 此方法用于初始化菜單,其中menu參數(shù)就是即將要顯示的Menu實(shí)例。 返回true則顯示該menu,false 則不顯示; 
   * (只會(huì)在第一次初始化菜單時(shí)調(diào)用) 
   */ 
  public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_main,menu); 
    return super.onCreateOptionsMenu(menu); 
  }
//設(shè)置菜單第二步:設(shè)置菜單按鈕 
    toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { 
      @Override 
      public boolean onMenuItemClick(MenuItem item) { 
        switch (item.getItemId()){ 
          case R.id.action_wlf: 
            Toast.makeText(MainActivity.this,"中國(guó)搏擊市場(chǎng)開(kāi)拓者",Toast.LENGTH_SHORT).show(); 
            break; 
          case R.id.action_klf: 
            Toast.makeText(MainActivity.this,"世界頂級(jí)站立式格斗賽事",Toast.LENGTH_SHORT).show(); 
            break; 
          case R.id.action_ufc: 
            Toast.makeText(MainActivity.this,"世界頂級(jí)無(wú)限制格斗賽事",Toast.LENGTH_SHORT).show(); 
            break; 
          case R.id.action_setting: 
            Toast.makeText(MainActivity.this,"掃一掃",Toast.LENGTH_SHORT).show(); 
            break; 
        } 
        return false; 
      } 
    });

看完上述內(nèi)容,你們掌握android中 ToolBar如何使用的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI