美文网首页
Toolbar使用备忘

Toolbar使用备忘

作者: 明慢慢 | 来源:发表于2016-11-08 14:08 被阅读8次
  1. Toolbar用于替换ActionBar,版本5.0开始采用,为保证向下兼容通常使用appcompat-v7兼容包。
  2. Toolbar和ActionBar冲突,要使用Toolbar需隐藏ActionBar,可在
    res/value/style.xml中设置
<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:表示根据屏幕的分辨率采用系统默认的高度
    如果低版本也要使用的话,则需要使用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>

相关文章

网友评论

      本文标题:Toolbar使用备忘

      本文链接:https://www.haomeiwen.com/subject/wbvbyttx.html