欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android ToolBar的簡單使用

 更新時間:2017年12月04日 11:40:24   作者:ZhengJiaoCsdn  
這篇文章主要為大家詳細介紹了android ToolBar的簡單使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了ToolBar的使用方法,供大家參考,具體內容如下

ToolBar時應用的標準工具欄;用來替代ActionBar;

使用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> 

接下來我們看下布局:

<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可以設置返回鍵、圖標、標題、副標題、菜單

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

返回鍵還可以設置監(jiān)聽:

toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        Toast.makeText(MainActivity.this,"你點擊了返回按鈕",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 
    會顯示在Item中,但是如果已經有4個或者4個以上的Item時會隱藏在溢出列表中。 
    當然個數(shù)并不僅僅局限于4個,依據屏幕的寬窄而定 
 
    never 
    永遠不會顯示。只會在溢出列表中顯示,而且只顯示標題,所以在定義item的時候,最好把標題都帶上。 
 
    always 
    無論是否溢出,總會顯示。 
 
    withText 
    withText值示意Action bar要顯示文本標題。Action bar會盡可能的顯示這個標題, 
    但是,如果圖標有效并且受到Action bar空間的限制,文本標題有可能顯示不全。 
 
    collapseActionView 
    聲明了這個操作視窗應該被折疊到一個按鈕中,當用戶選擇這個按鈕時, 
    這個操作視窗展開。否則,這個操作視窗在默認的情況下是可見的,并且即便在用于不適用的時候, 
    也要占據操作欄的有效空間。 
    --> 
  <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="武林風" 
    /> 
  <item 
    android:id="@+id/action_klf" 
    android:orderInCategory="100" 
    android:title="昆侖決" 
    /> 
</menu>

 然后在MainActivity眾設置菜單按鈕:

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

點擊打開鏈接免費下載源碼

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論