Drawer与Toolbar的简单使用(侧滑抽屉)--详解版
来源:互联网 发布:电信大数据营销 编辑:程序博客网 时间:2024/06/13 05:22
之前写个一篇Drawer与Toolbar的简单使用 - An_nA的博客 - CSDN博客 http://blog.csdn.net/an_nal/article/details/76174332解释Drawer的用法不是很详细,此帖子详细讲解一下。
布局文件样式:
1.整体分布
顶部自定义titlebar(可设置drawer开关图片,drawer所在的位置:左、右),中心布局样式自定义(此处为listview),右外侧drawer布局样式自定义(名为layout_search)。
2.自定义titlebar
1.布局:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true" android:elevation="5dp" android:background="@color/app_main"> <RelativeLayout android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="45dp"> <TextView android:paddingRight="10dp" android:visibility="gone" android:textSize="14sp" android:paddingLeft="15dp" android:gravity="center_vertical" android:drawableLeft="@mipmap/ic_back" android:layout_centerVertical="true" android:textColor="@color/app_white" android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="match_parent"/> <TextView android:id="@+id/title" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textSize="16sp" android:textColor="@color/app_white"/> <ImageView android:visibility="gone" android:layout_centerVertical="true" android:id="@+id/title_right_img" android:layout_alignParentRight="true" android:gravity="center" android:paddingLeft="10dp" android:paddingRight="20dp" android:layout_width="wrap_content" android:layout_height="match_parent" /> </RelativeLayout></RelativeLayout>
2.代码:
public class TitleBar extends RelativeLayout { private TextView back; private TextView title; private ImageView rightImg; public TitleBar(Context context) { super(context,null); } public TitleBar(Context context, AttributeSet attrs) { super(context, attrs); initView(context); } private void initView(Context context) { View view = LayoutInflater.from(context).inflate(R.layout.layout_titlebar, this,true); back = (TextView) view.findViewById(R.id.back); title = (TextView) view.findViewById(R.id.title); rightImg = (ImageView) findViewById(R.id.title_right_img); } public void setTitle(String str){ title.setText(str); } public void setBackText(String str){ back.setText(str);} public void setBackListener(OnClickListener listener){ back.setVisibility(VISIBLE); back.setOnClickListener(listener); } public void setRightImg(int resId,OnClickListener listener){ rightImg.setImageResource(resId); rightImg.setVisibility(VISIBLE); rightImg.setOnClickListener(listener); }}
3.整体界面
1.布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/app_gray_bg"><com.android.uoso.limspad.views.TitleBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/title_bar"/> <android.support.v4.widget.DrawerLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/drawer"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="right" android:orientation="vertical"> <include layout="@layout/layout_search" /> </LinearLayout> </android.support.v4.widget.DrawerLayout></LinearLayout>
注:
1.当在中心布局写了view(此处是listview)后,才会出现打开drawer后,中心是灰色的样式,若中心没有任何布局控件则无此效果。
2.titleBar的右侧图片无旋转动画,需要的自行添加哦。
2.代码:
给titleBar设置顶部标题、顶部右侧drawer开关图片、返回键监听事件:
titleBar.setTitle("页面顶部标题"); titleBar.setBackListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); titleBar.setRightImg(R.mipmap.ic_drawer, new View.OnClickListener() { @Override public void onClick(View v) { if (drawer.isDrawerOpen(Gravity.RIGHT)) { //右侧关闭drawer drawer.closeDrawer(Gravity.RIGHT); } else { //右侧打开drawer drawer.openDrawer(Gravity.RIGHT); } } });
阅读全文
0 0
- Drawer与Toolbar的简单使用(侧滑抽屉)--详解版
- Drawer与Toolbar的简单使用
- Android Drawer(抽屉,即侧拉栏)的简单使用:
- react-native中抽屉的使用(react-native-drawer)
- 简单导航抽屉(Navigation Drawer)
- Android: Drawer Layout(抽屉)侧滑页面
- 侧滑容器Drawer(抽屉式菜单)
- MaterialDrawer+ToolBar飘逸的导航和抽屉侧滑(用户登录信息的抽屉布局)
- 抽屉式侧滑菜单详解,drawer详解
- Android 导航抽屉---Navigation Drawer (DrawerLayout)使用
- Android的导航抽屉---Navigation Drawer
- Android的导航抽屉---Navigation Drawer
- Android Drawer抽屉布局的实现
- Android抽屉式导航栏drawerlayout与toolbar的共同使用
- Drawer Layout 与 Fragment的组合使用
- ToolBar的简单使用
- Toolbar的简单使用
- Toolbar 的简单使用
- linux设置代理
- java Lambda表达式Comparator和sort
- 【JavaEE学习笔记】MySQL_02_查询,约束
- 利用数据的商业智能BI分析工具
- Javascript Location测试
- Drawer与Toolbar的简单使用(侧滑抽屉)--详解版
- 课时5 gulp运行
- 使用SpringMVC创建Web工程并使用SpringSecurity进行权限控制的详细配置方法
- vi/vim 基本使用方法
- There is no Action mapped for namespace [/cargo/export]
- i2c总线 2
- Gradle错误(一)
- 指定tomcat使用特定版本jdk
- MYSQL中单引号,双引号,反引号的区别