ToolBar 和 DrawerLayout实现侧滑栏
来源:互联网 发布:给软件添加注册码 编辑:程序博客网 时间:2024/05/22 03:16
先实现ToolBar和TranslucentBar效果
layout/custom_toolbar.xml<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:id="@+id/toolbar" android:background="#600f" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试" android:textSize="20dp" android:textColor="#fff" /></android.support.v7.widget.Toolbar>
//在MainActivity.java 中toolBar = (Toolbar) findViewById(R.id.toolbar);setSupportActionBar(toolBar); //注意,要与DrawerLayout结合需要用这种方法实现toolBar,在Activity中的OnonCreateOptionsMenu()方法中实现菜单menutoolBar.setTitle("");toolBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.action_share: Toast.makeText(getApplicationContext(), "action_share", Toast.LENGTH_SHORT).show(); break; case R.id.action_edit: Toast.makeText(getApplicationContext(), "action_edit", Toast.LENGTH_SHORT).show(); break; case R.id.action_settings: Toast.makeText(getApplicationContext(), "action_settings", Toast.LENGTH_SHORT).show(); break; } return true; } }); @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main,menu); return true; }
layout/activity_main.xml中<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout 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" //以下两个属性是TranslucentBar实现需要 android:fitsSystemWindows="true" android:background="#600f" tools:context="com.linge.android.myapplication.MainActivity"> <include layout="@layout/content_main" /></android.support.design.widget.CoordinatorLayout>
layout/content_main.xml中<?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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.linge.android.myapplication.MainActivity" tools:showIn="@layout/activity_main" android:orientation="vertical" android:background="#fff" //把布局背景设置为白色 > <include layout="@layout/custom_toolbar"/> //drawlayout布局 <!--<include layout="@layout/drawlayout"/>--> </LinearLayout>
以上 toolBar设置完成
下面实现TranslucentBar效果
在values,values-v19,values-v21文件中设置自定义theme
values/styles.xml中<style name="TranslucentBarTheme" parent="AppTheme"> <item name="windowNoTitle">true</item> </style>
values-v19/styles.xml <style name="TranslucentBarTheme" parent="Theme.AppCompat.Light.DarkActionBar"><item name="android:windowTranslucentNavigation">true</item><item name="windowNoTitle">true</item></style>
values-v21/styles.xml中<style name="TranslucentBarTheme" parent="Theme.AppCompat.Light.DarkActionBar"><item name="android:windowTranslucentStatus">true</item><item name="android:windowTranslucentNavigation">true</item><item name="android:statusBarColor">@android:color/transparent</item><item name="windowNoTitle">true</item></style>
然后AndroidManifest.xml中需要设置的activity中设置相应的theme
例如
<activity android:name=".MainActivity" android:label="@string/app_name" //样式 android:theme="@style/TranslucentBarTheme"> ...</activity>
TranslucentBar设置完成
然后 结合DrawerLayout
drawlayout.xml文件中<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/drawlayout" >//主界面 <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/iv_pager"/> </RelativeLayout>//侧滑界面 <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#600f" android:layout_gravity="left"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv_left"></ListView> </RelativeLayout></android.support.v4.widget.DrawerLayout>
把上面提到的content_main.xml中注释打开 <include layout="@layout/custom_toolbar"/> //此处注释打开 <include layout="@layout/drawlayout"/>
mdrawLayout = (DrawerLayout) findViewById(R.id.drawlayout); lv = (ListView) findViewById(R.id.lv_left);//设置toolBar的HomeButton可用和显示//如果没有使用setSupportActionBar(...),getSupportActionBar()会为null getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); //获得抽屉开关,这个对象继承了抽屉状态的监听者,v7包下的对象,不是v4mToggle = new ActionBarDrawerToggle(this,mdrawLayout,toolBar, R.string.open,R.string.close){ @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); Toast.makeText(MainActivity.this,"open ",Toast.LENGTH_SHORT).show(); } @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); Toast.makeText(MainActivity.this,"close ",Toast.LENGTH_SHORT).show(); } };//把ActionBarDrawerToggle和DrawerLayout和toolbar关联并同步mToggle.syncState();mdrawLayout.setDrawerListener(mToggle); adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,lvs); lv.setAdapter(adapter);
1 0
- ToolBar 和 DrawerLayout实现侧滑栏
- Android使用DrawerLayout和ToolBar实现仿知乎侧滑菜单
- 利用ToolBar和Drawerlayout实现简单的侧滑菜单
- Toolbar + DrawerLayout实现侧滑
- Android ToolBar+DrawerLayout的实现
- Drawerlayout和ToolBar进行整合
- 使用Toolbar + DrawerLayout实现侧滑和改变toolbar左边按钮颜色
- DrawerLayout,Toolbar
- DrawerLayout + Toolbar
- ToolBar + DrawerLayout
- Drawerlayout Toolbar
- Toolbar+DrawerLayout
- DrawerLayout+ToolBar
- Toolbar + drawerlayout 实现侧滑菜单
- Toolbar结合DrawerLayout实现的抽屉菜单
- Toolbar + DrawerLayout快速实现高大上菜单侧滑
- NavigationView+Drawerlayout+Toolbar实现侧滑菜单
- ToolBar+DrawerLayout实现MD设计效果
- ListView列表
- (OpenCV)笔记汇总
- iOS开源弹幕库BarrageRenderer
- 软件需求分析模板
- 快速排序
- ToolBar 和 DrawerLayout实现侧滑栏
- 欢迎使用CSDN-markdown编辑器
- Fresco用法总结基础篇
- C++ Unicode SBCS 函数对照表
- Play framework REST API
- 查看当前用户的一些权限
- RF射频技术-si4438C芯片介绍
- OC语言实现指纹识别
- nw.js node-webkit系列(21)在应用中如何数据持久化