Android ToolBar简单记录

来源:互联网 发布:天刀捏脸刘亦菲数据 编辑:程序博客网 时间:2024/05/17 18:43

只是个人的简单记录 已经补充 方便日后使用
详细的讲解请看原文:
Android Material Design之Toolbar与Palette实践
Android 5.x Theme 与 ToolBar 实战 5.x Theme 与 ToolBar 实战

步骤

1:values/styles.xml 修改为

若 parent=”Theme.AppCompat.NoActionBar”的话 上面的字体就是白色的 但是对其他效果的颜色不好解决。
所以把 toolbar的theme改成 下面 就可以试下toolbar的字体颜色为白色
app:theme=”@style/ThemeOverlay.AppCompat.Dark.ActionBar”

<resources>    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">        <item name="colorPrimary">#3F51B5</item>        <item name="colorPrimaryDark">#303F9F</item>        <item name="android:windowBackground">#FFFFFF</item>    </style>    <style name="AppTheme" parent="AppBaseTheme"></style></resources>

2:新建一个 toolbar.xml toolbar的布局文件 便于重复使用(需要自己的布局可以在这里添加)

<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.Toolbar    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res/com.bilue.demo"    android:id="@+id/toolbar"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="?attr/colorPrimary"    android:minHeight="?attr/actionBarSize"    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"    app:theme="@style/ThemeOverlay.AppCompat.ActionBar"    ></android.support.v7.widget.Toolbar>

3: 添加到布局中

(第一种是 侧滑菜单在 toolbar 上面 第二种是覆盖toolbar)

第一种

<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=".MainActivity"    android:orientation="vertical"    >    <include layout="@layout/toolbar" />    <android.support.v4.widget.DrawerLayout        android:id="@+id/drawer"        android:layout_width="match_parent"        android:layout_height="match_parent" >        <!-- 内容界面 -->        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical" >            <TextView                android:layout_width="match_parent"                android:layout_height="match_parent"                android:text="test"                />        </LinearLayout>        <!-- 侧滑菜单内容 -->        <LinearLayout            android:id="@+id/drawer_view"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_gravity="left"            android:orientation="vertical"            android:background="#ffffff"            >            <TextView                android:layout_width="match_parent"                android:layout_height="match_parent"                android:text="test"                />        </LinearLayout>    </android.support.v4.widget.DrawerLayout></LinearLayout>

第二种

<android.support.v4.widget.DrawerLayout    android:id="@+id/drawer"    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=".MainActivity"    android:orientation="vertical"    >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        >        <include layout="@layout/toolbar" />        <!-- 内容界面 -->        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical" >            <TextView                android:layout_width="match_parent"                android:layout_height="match_parent"                android:text="test"                />        </LinearLayout>    </LinearLayout>        <!-- 侧滑菜单内容 -->        <LinearLayout            android:id="@+id/drawer_view"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_gravity="left"            android:orientation="vertical"            android:background="#ffffff"            >            <TextView                android:layout_width="match_parent"                android:layout_height="match_parent"                android:text="test"                />        </LinearLayout></android.support.v4.widget.DrawerLayout>

4: 代码

import android.app.Notification;import android.support.v4.widget.DrawerLayout;import android.support.v7.app.ActionBarDrawerToggle;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.Toolbar;import android.view.MenuItem;public class MainActivity extends AppCompatActivity {    private Toolbar toolbarView;    private DrawerLayout drawerView;    private ActionBarDrawerToggle drawerToggle;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        init();    }    private void init(){        toolbarView = (Toolbar) findViewById(R.id.toolbar);        setSupportActionBar(toolbarView);        drawerView = (DrawerLayout) findViewById(R.id.drawer);        drawerToggle = new ActionBarDrawerToggle(this,drawerView,toolbarView,R.string.draw_open,R.string.draw_close);        //显示菜单        drawerToggle.syncState();        //显示动画        drawerView.setDrawerListener(drawerToggle);    }}

更炫的 Material Design 抽屉

只作记录 方便下次用 所以详细讲解见
Design Support Library (I): Navigation View的使用
Android 自己实现 NavigationView [Design Support Library(1)]

其中 compile ‘com.android.support:design:22.2.0’ 的版本 跟着com.android.support:appcompat-v7的版本写即可
否则会出现

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ais.cherry/com.ais.cherry.activity.FirstActivity}: android.view.InflateException: Binary XML file line #32: Error inflating    class android.support.design.widget.NavigationView

ToolBar底下的菜单条目(Tabs)

配合 ViewPager FragmentPagerAdapter

详细讲解见

TabLayout of design support library

补充 备注

想看其他的style属性可进入TabLayout里面的构造函数看 比如点击后的字体颜色

 if (a.hasValue(R.styleable.TabLayout_tabSelectedTextColor)) {            // We have an explicit selected text color set, so we need to make merge it with the            // current colors. This is exposed so that developers can use theme attributes to set            // this (theme attrs in ColorStateLists are Lollipop+)            final int selected = a.getColor(R.styleable.TabLayout_tabSelectedTextColor, 0);            mTabTextColors = createColorStateList(mTabTextColors.getDefaultColor(), selected);        }

即是添加 tabSelectedTextColor 属性即可

0 0