【UI设计】DrawerLayout使用
来源:互联网 发布:淘宝网店商品上架 编辑:程序博客网 时间:2024/05/21 10:34
1、简介:
侧滑两种效果:1.盖在整个页面上面;2.在Toolbar下面。在MD提出来以后,谷歌就收录并改变了很多开源项目,放到API及support包里面。DrawerLayout 抽屉容器 来自support-v4包里面的。(android.support.v4.widget) 相当于一个自定义容器 extends ViewGroup ,可以看出是一个有侧滑效果的帧布局 两个部分:1)内容布局;2)侧滑出来的菜单布局
2、使用代码:
public class MainActivity extends AppCompatActivity{ private Toolbar toolbar; private DrawerLayout drawerlayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); toolbar = (Toolbar)findViewById(R.id.toolbar); //将actionBar替换成toolbar setSupportActionBar(toolbar); drawerlayout = (DrawerLayout)findViewById(R.id.drawerlayout); ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerlayout, toolbar, R.string.drawer_open, R.string.drawer_close); //同步状态 drawerToggle.syncState(); //给侧滑控件设置监听 drawerlayout.setDrawerListener(new DrawerListener() { @Override public void onDrawerStateChanged(int newState) { // 状态发生改变 } @Override public void onDrawerSlide(View drawerView, float slideOffset) { // 滑动的过程当中不断地回调 slideOffset:0~1 View content = drawerlayout.getChildAt(0); View menu = drawerView; float scale = 1-slideOffset;//1~0 float leftScale = (float) (1-0.3*scale); float rightScale = (float) (0.7f+0.3*scale);//0.7~1 menu.setScaleX(leftScale);//1~0.7 menu.setScaleY(leftScale);//1~0.7 content.setScaleX(rightScale); content.setScaleY(rightScale); content.setTranslationX(menu.getMeasuredWidth()*(1-scale));//0~width } @Override public void onDrawerOpened(View drawerView) { // 打开 } @Override public void onDrawerClosed(View drawerView) { // 关闭 } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
XML:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawerlayout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 内容部分的布局 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff0" android:text="我是内容部分" /> </LinearLayout> <!-- 侧滑菜单左侧部分 --> <!-- 通过layout_gravity=start|end设置左右布局 --> <LinearLayout android:layout_width="200dp" android:layout_height="fill_parent" android:layout_gravity="start" android:background="#0f0" android:orientation="vertical" android:paddingTop="50dp" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0" android:text="item1" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0" android:text="item2" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0" android:text="item3" /> </LinearLayout> <!-- 侧滑菜单右侧部分 --> <LinearLayout android:layout_width="200dp" android:layout_height="fill_parent" android:layout_gravity="end" android:background="#0f0" android:orientation="vertical" android:paddingTop="50dp" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0" android:text="item1" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0" android:text="item2" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0" android:text="item3" /> </LinearLayout></android.support.v4.widget.DrawerLayout>
相比官方封装的NavigationView来说,具有更好的拓展性。
阅读全文
0 0
- 【UI设计】DrawerLayout使用
- 左边栏UI布局与抽屉(DrawerLayout)的使用
- Android UI之DrawerLayout
- android--UI---侧滑drawerLayout
- Android UI 备忘:DrawerLayout
- DrawerLayout 使用
- DrawerLayout 使用
- DrawerLayout使用
- DrawerLayout使用
- DrawerLayout使用
- drawerlayout使用
- [UI]抽屉菜单DrawerLayout分析
- UI设计:使用OODA循环
- DrawerLayout的基本使用
- Android - DrawerLayout的使用
- DrawerLayout的基本使用
- DrawerLayout控件的使用
- 学习使用DrawerLayout
- 视频类搜索引擎优化教程侧重点
- Java学习笔记(5)——泛型程序设计
- #关于VS2013中遇到的问题总结
- Java 上传文件
- 干货丨各种机器学习任务的顶级结果(论文)汇总
- 【UI设计】DrawerLayout使用
- 干货丨机器学习/算法19家公司面试心经(内含薪资待遇)
- eclipse通过git导入网站项目
- Insights2.1配置访问Oracle数据库表
- linux查看系统编码和修改系统编码
- java base64编码和解码
- c++ vector(向量)使用方法详解
- C#对XML文件的各种操作实现方法
- 自定义函数实现mb_strlen与中文 strrev转置功能