Android-自定义Toolbar的方法
来源:互联网 发布:java编写syslog 编辑:程序博客网 时间:2024/04/25 17:42
因为实际开发一个App需要用自定义的Toolbar/Actionbar ,网上查了很多资料发现写的并不全,今天通过实际编写代码总结出一个比较简单方便的自定义toolbar的方法,仅供参考
1,toolbar修改后退键图标的方法
获得Toolbar的实例,直接使用setNavigationIcon(resId)即可改变原来的后退按钮图标
toolbar.setNavigationIcon(R.drawable.button_login);
2,自定义Toolbar的样式
例:将toolbar的title改到中间:
->MainActivity中取消显示Title
getSupportActionBar().setDisplayShowTitleEnabled(false);
->在Toolbar内部加入TextView用来当作title显示,代码如下,我添加了一个TextView显示Title,一个ImageView显示刷新的图标
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
app:contentInsetLeft="0dp"
app:popupTheme="@style/Theme.AppCompat.Light"
app:contentInsetStart="0dp"
android:theme="@style/Theme.Design.NoActionBar" >
<TextView
android:id="@+id/toolbar_title"
android:text="@string/title_yun_note"
android:layout_gravity="center"
android:textSize="20sp"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="@drawable/navigation_refresh"
android:id="@+id/image_button_refresh"
android:layout_gravity="right"
android:visibility="invisible"
android:layout_marginRight="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
app:contentInsetLeft="0dp"
app:popupTheme="@style/Theme.AppCompat.Light"
app:contentInsetStart="0dp"
android:theme="@style/Theme.Design.NoActionBar" >
<TextView
android:id="@+id/toolbar_title"
android:text="@string/title_yun_note"
android:layout_gravity="center"
android:textSize="20sp"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="@drawable/navigation_refresh"
android:id="@+id/image_button_refresh"
android:layout_gravity="right"
android:visibility="invisible"
android:layout_marginRight="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
->以上这种方式可以完全自定义toolbar的所有控件,自定义的控件可以通过MainActivity::toolbar.findViewById()设置事件的监听,代码如下
refreshButton = (ImageView) toolbar.findViewById(R.id.image_button_refresh);
refreshButton.setVisibility(View.VISIBLE);
refreshButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO: 16/8/13 rotate refresh button
// TODO: 16/8/13 check isLogin, if not, go to LoginActivity
Toast.makeText(MainActivity.this,"rotate refresh button",Toast.LENGTH_SHORT).show();
}
});
refreshButton.setVisibility(View.VISIBLE);
refreshButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO: 16/8/13 rotate refresh button
// TODO: 16/8/13 check isLogin, if not, go to LoginActivity
Toast.makeText(MainActivity.this,"rotate refresh button",Toast.LENGTH_SHORT).show();
}
});
3,以上的Toolbar自定义样式可以与res\menu\menu.xml 一起用,即
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}
这样你的toolbar就可以自定义标题样式以及控件,同时又可以用overflow了。
关于Toolbar的基本使用请查看
http://blog.csdn.net/githan/article/details/52137146
1 1
- Android-自定义Toolbar的方法
- android的自定义toolbar
- Toolbar的使用以及自定义Toolbar的方法
- android自定义toolbar的menuItem图标大小
- Android 自定义Toolbar的Menu菜单
- Android自定义ToolBar布局
- android 自定义Toolbar使用
- android studio自定义toolbar
- android自定义Toolbar
- android---自定义ToolBar样式
- 自定义ToolBar的实现
- Toolbar的完全自定义
- Toolbar的完全自定义
- 自定义ToolBar的样式
- 自定义view的toolbar
- 最简单的自定义Toolbar SearchView样式方法
- 最简单的自定义Toolbar SearchView样式方法
- 自定义toolbar和原生toolbar的比较
- 80c51用计算器程序
- 1591---交叉排序
- Android OKHttp介绍与使用(二)
- 2016.08.13【初中部 NOIP提高组 】模拟赛C题解
- 字典树(Codeforces Round #367 (Div. 2) Vasiliy's Multiset,Xor问题 )
- Android-自定义Toolbar的方法
- 可持久化线段树
- 炫丽的倒计时效果
- http服务-建立一个简单http客户端
- Linux文件系统中/bin、/sbin、/usr/bin、/usr/sbin、/usr/local/bin、/usr/local/sbin文件夹的区别是什么
- HDU 1045-Fire Net
- 关于windows系统安装jdk时出现的一些问题的解决方法
- 理解通用定时器
- 古人的作息,该上朝了