Android--Toolbar实现标题栏

Android自带的那个Actionbar是不是感觉有点low,2014年Google推出了Material这种精美界面设计,而Toolbar便是其中的一个控件,用于制作自己的标题。
效果:
在这里插入图片描述
Toolbar还可以做出更好的效果但这里至介绍其中的几种。
制作步骤如下:

先隐藏掉原来的标题

打开/value/styles.xml文件修改代码

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
	<!--将原代码修改成如上所示-->
	...

在布局中加入toolbar控件

布局中要先加入命名空间

xmlns:app=“http://schemas.android.com/apk/res-auto

...
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
<FrameLayout
	android:id="@+id/Layout_add"
	android:layout_width="match_parent"
	android:layout_height="match_parent">

	<android.support.v7.widget.Toolbar
		android:id="@+id/toolbar"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:background="@color/pink"
		android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
		app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</FrameLayout>

修改activity中的代码

protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_add);
	Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
	setSupportActionBar(toolbar);  //加载Toolbar控件
	...

这样Toolbar标题算是做出来了,但是有点单调。我们还可以设置Toolbar其他元素,常用的元素大抵有这些:

  • setNavigationIcon
    设定顶部左边导航的图标。
  • setLogo
    APP 的图标。
  • setTitle
    主标题。
  • setSubtitle
    副标题。
  • setOnMenuItemClickListener
    设定菜单各按鈕的动作的监听器。

setNavigationIcon

我们示例一下setNavigationIcon。先在drawable保存好图标。在activity中加入代码

	Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
	setSupportActionBar(toolbar);
	getSupportActionBar().setDisplayShowTitleEnabled(false);//隐藏标题
	toolbar.setNavigationIcon(R.drawable.back_white);       //加载图标
	toolbar.setNavigationOnClickListener(new View.OnClickListener() {   //为图标设置监听器
		@Override
		public void onClick(View v) {
			finish();
		}
	});

标题居中

找了一下Toolbar标题居中的办法最通用的还是讲原来标题隐藏掉,然后做一个TextView做标题。
上面setNavigationIcon我们已经把标题隐藏了。只要往布局加入TextView就行了。这里另外添加了一张图和文字一起作为标题。

<TextView
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:layout_gravity="center_horizontal"
	android:layout_marginTop="9dp"
	android:layout_marginLeft="10dp"
	android:text=" Note"
	android:textColor="@color/white"
	android:textSize="30dp" />

<ImageView
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:layout_gravity="center_horizontal"
	android:layout_marginTop="14dp"
	android:layout_marginRight="30dp"
	android:src="@drawable/i_white2" />

设置菜单

设置菜单和Actionbar基本一样的,区别在于菜单xml文件中也需加入命名空间

xmlns:app=“http://schemas.android.com/apk/res-auto

另外还有一个要讲的菜单属性app:showAsAction=“always”,这里设置菜单action图标在标题中显示。当然这里不一定是always。还可以是如下设置。

  • always 一直显示在Toolbar中
  • ifRoom 屏幕空间足够时显示在Toolbar
  • never 只显示在菜单中
  • 需要注意,Toolbar中action只显示图标,菜单中只显示文字。
  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值