Toolbar入门

1引入

使用之前先将自带的ActionBar屏蔽,清单文件

<application
	android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

android:implementation 'com.android.support:appcompat-v7:27.0.2
androidx:implementation 'androidx.appcompat:appcompat:1.2.0’里包含了

2常见的用法

2.1xml

  1. android:layout_height="?attr/actionBarSize"(高度为状态栏高度)
  2. android:background(背景)
  3. app:navigationIcon(标题左边icon)
  4. app:title(标题)
  5. app:titleTextColor(标题颜色)
  6. app:navigationIcon(最左侧icon)
  7. titleMarginStart(标题距离(开始)左边的距离)
  8. app:subtitle(子标题,标题的下方)
  9. app:titleTextAppearance(自定义标题style样式)

2.2 java

Toolbar toolbar=findViewById(R.id.toolbar);
toolbar.setTitle("标题");//标题
toolbar.setTitleTextColor(Color.RED);//标题颜色
toolbar.setNavigationIcon(R.drawable.back);//标题左边icon
toolbar.setNavigationOnClickListener(v -> {
//点击左边返回按钮监听事件
});

3花式使用

3.1标题居中

Toolbar中添加textview并设置为android:layout_gravity="center"

3.2结合menu

1创建menu文件

在src的menu文件夹下增加xml文件名字看个人

 <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_search"
        android:title="Search"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_settings"
        android:icon="@mipmap/ic_launcher"
        android:orderInCategory="100"
        android:title="@string/str_settings"
        app:showAsAction="never" />
</menu>
2代码中加载
toolbar.inflateMenu(R.menu.menu);
3设置弹出的菜单样式:
<style name="ToolbarPopupTheme" parent="@style/ThemeOverlay.AppCompat.Dark">
         <!--<item name="android:colorBackground">#000000</item>--> <!--这里可以改变菜单的背景色-->
        <item name="actionOverflowMenuStyle">@style/OverflowMenuStyle</item> <!--新增一个item,用于控制menu-->
    </style>

    <style name="OverflowMenuStyle" parent="Widget.AppCompat.Light.PopupMenu.Overflow">
        <item name="overlapAnchor">false</item>  <!--把该属性改为false即可使menu位置位于toolbar之下-->
    </style>

在toolBar中引用
app:popupTheme=”@style/ToolbarPopupTheme”

4menu介绍

4.app:showAsAction有以下三个属性:

  1. ifRoom 表示在屏幕空间足够的情况下显示在Toolbar中,不够的话就显示在菜单中
  2. never 表示永远不显示在Toolbar中,而是一直显示在菜单中
  3. always 表示永远显示在Toolbar中,如果屏幕空间不够则不显示

注:Toolbar中的action按钮只会显示图标,菜单中的action按钮只会显示文字。

3.3与AppBarLayout结合

引入:implementation 'com.google.android.material:material:1.2.1'

<com.google.android.material.appbar.AppBarLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content">
   <androidx.appcompat.widget.Toolbar
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@color/purple_200"
       app:layout_scrollFlags="scroll"
       app:popupTheme="@style/ToolbarPopupTheme">
   </androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
1)app:layout_scrollFlags=“scroll”

滚动布局滑动到顶端时,标题栏toolbar才会显示

2)app:layout_scrollFlags=”scroll|enterAlways”

toolbar会根据当前的滚动距离,自动选择是隐藏还是显示

3)app:layout_scrollFlags=”scroll|enterAlways|exitUntilCollapsed”

布局

<androidx.appcompat.widget.Toolbar
	android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@color/colorAccent"
    android:minHeight="40dp"
    app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed"
    app:popupTheme="@style/ToolbarPopupTheme">
    </androidx.appcompat.widget.Toolbar>

效果:

向上滑动时高度达到最小高度时ToolBar不再滑动。向下滑动则会全部显示

4)app:layout_scrollFlags=”scroll|enterAlways|enterAlwaysCollapsed”

布局与上面相似。效果:

上滑的时候,toolbar直到完全隐藏时,下面的滚动布局才开始发生滚动;下滑时toolbar会优先滑出设置的最小高度值,然后当滚动布局下滑到顶部时,后面的toolbar部分,才开始完全显示

3.4与CollapsingToolbarLayout结合的使用

<com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="180dp">
        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll">
            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="180dp"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_action_accept"
                app:layout_collapseMode="pin" />
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:navigationIcon="@drawable/ic_action_accept"
                app:popupTheme="@style/Theme.AppCompat.NoActionBar"
                app:title="标题"
                app:titleTextColor="@color/white">
            </androidx.appcompat.widget.Toolbar>
        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>

<!--transparentText (透明)样式-->
<style name="transparentText" parent="TextAppearance.AppCompat.Small">
    <item name="android:textColor">#00000000</item>
    <item name="android:textSize">15sp</item>
</style>
<!--ToolbarTitle样式-->
<style name="ToolbarTitle" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textColor">#f0f0</item>
    <item name="android:textSize">15sp</item>
</style> 

CollapsingToolbarLayout属性

  1. app:contentScrim( 当上滑到toolbar 高度期间直到达到toolbar高度时,给toolbar设置的背景色,以及过渡颜色或图片。)
  2. app:layout_scrollFlags(与Toolbar 相同)
  3. app:expandedTitleGravity(展示后title的位置)
  4. app:expandedTitleMarginStart(展示后title 距离开始位置的边距)
  5. app:collapsedTitleGravity(收缩后title 位置)
  6. app:expandedTitleTextAppearance(展开后标题文字的样式)
  7. app:collapsedTitleTextAppearance(折叠后标题文字的样式)
  8. app:layout_collapseMode参数。(1)parallax:有该标志位的View在页面滚动的过程中会一直停留在顶部,比如Toolbar被固定在顶部,(2)pin:有该标志位的View表示能和页面同时滚动。
  9. imgview的layout_collapseParallaxMultiplier。该属性是视差因子,表示该View与页面的滚动速度存在差值,造成一种相对滚动的效果。

状态栏透明5.0以上java代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getWindow();
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
            | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(Color.TRANSPARENT);
    window.setNavigationBarColor(Color.TRANSPARENT);
}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    Window window = getWindow();
    window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}

5.0以下,4.4以上
在res下新建values-v19这个文件夹,然后新建styles

<resources>
    <style name="ImageTranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>

布局xml根节点

android:fitsSystemWindows=“true”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值