歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中Toolbar的基本使用

Android中Toolbar的基本使用

日期:2017/3/1 9:10:29   编辑:Linux編程

Android的標題欄是很重要的一個模塊,App是否易用很大一部分要看標題欄。寫這個博客的時候剛發現谷歌推出了一種新的標題欄實現方式。
它相對於以前的ActionBar來說,最大的變化是開發者可以在標題欄上增加自定義的view。同時在最左端添加了一個導航按鈕。

  1. 將Activity的默認標題欄禁用。
    這個實現有兩中方式,代碼控制和xml文件裡配置
    代碼
    如果是繼承 AppCompatActivity調用 supportRequestWindowFeature(Window.FEATURE_NO_TITLE) 去 掉默認的導航欄。
    如果是繼承Activity就應該調用 requestWindowFeature(Window.FEATURE_NO_TITLE) );
    xml文件配置
    給Activity設置沒有默認標題欄的主題NoActionBar
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  1. 在布局文件中添加Toolbar控件,注意要引用v7包,另外需要在布局文件中加入xmlns:app=”http://schemas.android.com/apk/res-auto”,然後才可以通過app:XXXXXXX來配置標題欄的一些屬性,如下面的 app:popupTheme=”@style/AppTheme.PopupOverlay” 給標題欄設置了主題。
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>
  1. 在menu文件夾下添加menu選項的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.administrator.gattbluethoothdemo.MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="設置"
        app:showAsAction="never" />
    <item android:id="@+id/menu_refresh"
        android:title="更新"
        android:checkable="false"
        android:orderInCategory="1"
        app:showAsAction="ifRoom"/>
    <item android:id="@+id/menu_scan"
        android:title="掃描"
        android:orderInCategory="100"
        app:showAsAction="ifRoom|withText"/>
    <item android:id="@+id/menu_stop"
        android:title="停止"
        android:orderInCategory="101"
        app:showAsAction="ifRoom|withText"/>
</menu>
  1. 在Activity中定義使用Toolbar
    完成上面的准備工作,我們就能開始在java代碼中添加和調用Toolbar控件了。
    在onCreate方法中添加
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("掃描界面");
        toolbar.setSubtitle("子標題");
        //設置導航欄圖標
        toolbar.setNavigationIcon(R.mipmap.ic_launcher);
        //設置程序logo
        toolbar.setLogo(R.mipmap.ic_launcher);
        //設置Toolbar像ActionBar一樣實現,
        setSupportActionBar(toolbar);

接著就和ActionBar一樣,

   @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;
    }

    @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 SimplifiableIfStatement
        if (id == R.id.action_settings) {
           Toast.makeText(MainActivity.this,"請設置",Toast.LENGTH_SHORT).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

看一下效果圖

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved