Toolbar使用備忘

  1. Toolbar用于替換ActionBar,版本5.0開始采用,為保證向下兼容通常使用appcompat-v7兼容包。
  2. Toolbar和ActionBar沖突,要使用Toolbar需隱藏ActionBar,可在
    res/value/style.xml中設(shè)置
<resources>
 <!-- Base application theme. --> 
<style name="AppTheme" parent="AppTheme.Base"></style> 
<style name="AppTheme.Base" parent="Theme.AppCompat"> 
<!--將ActionBar隱藏,這里使用ToolBar--> 
<item name="windowActionBar">false</item>
<!-- 使用 API Level 22以上編譯的話,要拿掉前綴字 -->
<item name="windowNoTitle">true</item>
 </style>
 <!--Status bar color-->
 <color name="statusColor">#ff0000</color> 
<!-- toolBar color -->
 <color name="toolBarColor">#0000ff</color> 
<!--EditText,RadioButton,CheckBox color-->
 <color name="editColor">#FD87A9</color> 
<!--Window color--> 
<color name="widowColor">#ffffff</color>
</resources>
  1. 布局文件簡單描述
<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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <!--
    ?attr/actionBarSize:表示根據(jù)屏幕的分辨率采用系統(tǒng)默認(rèn)的高度
    如果低版本也要使用的話,則需要使用v7包的,否則只有api21上才能有效
    -->
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary">
        <!--添加Toolbar的子控件-->
        <Button
            android:id="@+id/btn_diy"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:background="#80ffffff"
            android:text="自定義按鈕"
            android:textColor="#000000"
            android:textSize="11sp" />
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="首頁"
            android:textColor="@android:color/black"
            android:textSize="20sp" />
    </android.support.v7.widget.Toolbar>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/hello_world"
        android:textColor="@android:color/black"
        android:textSize="30sp" />
</LinearLayout>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容