DrawerLayout是实现侧滑的布局,它的方便性在于它自带侧滑监听,由于很早就想实现一下像QQ一样的侧滑菜单,于是今天研究了一下DrawerLayout这个布局,废话少说我们先看一下布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="11111111111111111"/> </LinearLayout> <RelativeLayout android:layout_width="50dp" android:layout_height="match_parent" android:layout_gravity="left" android:background="@color/colorAccent" > </RelativeLayout> </android.support.v4.widget.DrawerLayout>
在解释这个布局文件之前首先要说一下,DrawerLayout里面的第一个子布局就是我们进入app看到的布局,而第二个布局就是侧滑而出的布局,而第二个布局里面最重要的属性就是layout_gravity这条属性了,left表示从左边划出,right表示右边。然后现在我们将程序启动,就可以发现已经实现了侧滑功能了,这里着重强调一点就是这个侧滑的布局的高度必须是match_parent,不然的话就会报错的哟。
然后让我们来看一看效果图:
哇塞,好丑。。嗯。。所以改进一下。。,一般来说侧滑的布局都应该是列表,但是这里为了节约时间,我就不弄列表了。
把相对布局改成线性布局,然后在里面加上两个textView:
使用这个来测试使用一下当我们点击两个不同的textview的时候改变主页面内的textview的内容
点击第二个item的时候将值传给主界面的textview,并且自动关闭DrawerLayout,先上代码:
package com.example.administrator.drawerlayouttest; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView item1, item2, home; private DrawerLayout drawerLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout=findViewById(R.id.drawerlayout_main); item1 = findViewById(R.id.item_textview_main_1); item2 = findViewById(R.id.item_textview_main_2); home = findViewById(R.id.home_textview_main); item1.setOnClickListener(this); item2.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.item_textview_main_1: home.setText(item1.getText().toString()); drawerLayout.closeDrawers();//设置Drawerlayout关闭 break; case R.id.item_textview_main_2: home.setText(item2.getText().toString()); drawerLayout.closeDrawers(); break; } } }
很简单的设置点击事件和赋值操作,然后实现上图的功能,这里有一个很重要的东西,就是如果你的DrawerLayout的侧滑菜单的布局写在了主页布局的上面,那么点击事件就会失败(下图),这里亲自实验得到了证实。
(下面是错误示范)
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout android:id="@+id/drawerlayout_main" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <LinearLayout android:layout_width="200dp" android:layout_height="match_parent" android:layout_gravity="start" android:orientation="vertical" android:background="#ffff"> <TextView android:id="@+id/item_textview_main_1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="11111" /> <TextView android:id="@+id/item_textview_main_2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="22222" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/home_textview_main" android:layout_width="match_parent" android:layout_height="match_parent" android:text="11111111111111111" /> </LinearLayout> </android.support.v4.widget.DrawerLayout>
如果是这种布局的话,每次点击菜单,菜单都会自动调用closeDrawers方法,具体原因我也不知道,官方文档反正就是说写在下面。
如果有什么错误欢迎指出。
最后
以上就是有魅力白猫最近收集整理的关于Android—— DrawerLayout 学习的全部内容,更多相关Android——内容请搜索靠谱客的其他文章。
发表评论 取消回复