表示之前都不想写博客了,但是今天突然遇到一个问题,就是关于toolbar的,然后就在这个问题上浪费了半天的时间。
所以决定还是写写博客记录一下,以防以后遇到相同的问题,在浪费时间,也给大家一些参考吧。
首先,我当时需要解决的问题是,让Toolbar显现出自带的后退按钮。因为之前写的自定义的后退按钮,去掉了原有的标题栏,导致一个很简单的问题变的复杂了。
说一下,现在我知道的实现(有部分不是太懂,所以只讲整体实现作用)
下面第一个部分,实现 将状态栏的背景设为当前页面的背景,需要最低api19
其中前两个设置颜色的,可以根据需求,设置该项目所需的主题颜色
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="actionMenuTextColor">#ffffff</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowTranslucentStatus">true</item> <item name="colorControlNormal">@android:color/white</item>
</style>
实现标题居中
1、创建一个Toolbar,之后将内容置为空,再在其中添加一个TextView,可以根据自己的需求进行设置。
我下面的实现 字体颜色白色 ,大小20sp ,居中;内容可以在后台根据页面的需求自定义设置。
其中想要使用自定义返回按钮可以通过添加ImageView,或者在Toolbar 中进行设置(这里没写)
在需要标题的页面,通过include引入
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorPrimaryDark" android:fitsSystemWindows="true" > <!--<ImageView--> <!--android:id="@+id/back_image"--> <!--android:layout_width="30dp"--> <!--android:layout_height="30dp"--> <!--android:src="@mipmap/back"--> <!--/>--> <TextView android:id="@+id/txt_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_gravity="center" android:textColor="@android:color/white" android:textSize="20sp" /> </android.support.v7.widget.Toolbar>
引入
<include layout="@layout/common_tool_bar" />
下面讲的就是显示自带的后退按钮
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
这句是显示按钮,点击事件需要自定义。
在写这个之前还需要写
toolbar.setTitle(""); setSupportActionBar(toolbar);
不过一般这样写都会报错。像我就是这样。
需要在主题中添加下面两句。
<item name="windowActionBar">false</item> <item name="windowNoTitle">true</item>
现在讲一下整体的
styles中
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="actionMenuTextColor">#ffffff</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowTranslucentStatus">true</item> <item name="colorControlNormal">@android:color/white</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style>
布局
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorPrimaryDark" android:fitsSystemWindows="true" > <!--<ImageView--> <!--android:id="@+id/back_image"--> <!--android:layout_width="30dp"--> <!--android:layout_height="30dp"--> <!--android:src="@mipmap/back"--> <!--/>--> <TextView android:id="@+id/txt_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_gravity="center" android:textColor="@android:color/white" android:textSize="20sp" /> </android.support.v7.widget.Toolbar>
Activity
Toolbar toolbar=findViewById(R.id.toolbar);
toolbar.setTitle(""); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
点击事件
toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } });