有些安卓应用喜欢用侧滑菜单来作为用户信息页的入口,安卓官方提供了一个侧滑菜单控件——DrawerLayout,来实现这种效果,今天通过一个简单的demo来学习一下这个控件,首先来看看这个控件实现的简单效果:
![](https://img.haomeiwen.com/i17127000/3846c69ed2922c31.gif)
此demo实现的功能就是,打开DrawerLayout,点击按钮主布局切换对应的fragment,下面先贴上布局:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/drawer_layout">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_container"/>
<LinearLayout
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#8e8e8e"
android:orientation="vertical"
android:id="@+id/drawer">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment one"
android:id="@+id/btn_one"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment two"
android:id="@+id/btn_two"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment three"
android:id="@+id/btn_three"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
布局上有三点需要注意:
1、带有DrawerLayout的activity或者fragment要以DrawerLayout为根布局;
2、布局下第一个View为主布局,就是DrawerLayout消失显示的布局,第二个View就是DrawerLayout了;
3、一般DrawerLayout在不使用时或者初次进入页面时是隐藏起来的,通过滑动显示出来,所以在Drawer的布局中需要加上属性android:layout_gravity
,例如例子中我是放在左边的,就设置为android:layout_gravity="start"
。
下面是业务代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private DrawerLayout mDrawerLayout;
private LinearLayout mDrawer;
private Button mBtnOne;
private Button mBtnTwo;
private Button mBtnThr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawer = findViewById(R.id.drawer);
mBtnOne = findViewById(R.id.btn_one);
mBtnTwo = findViewById(R.id.btn_two);
mBtnThr = findViewById(R.id.btn_three);
mBtnOne.setOnClickListener(this);
mBtnTwo.setOnClickListener(this);
mBtnThr.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_one:
replaceFragment(1);
break;
case R.id.btn_two:
replaceFragment(2);
break;
case R.id.btn_three:
replaceFragment(3);
break;
}
}
/**
* 切换fragment
*/
private void replaceFragment(int id){
FragmentManager fm = getSupportFragmentManager();
switch (id){
case 1:
fm.beginTransaction().replace(R.id.fragment_container,new FirstFragment()).commit();
break;
case 2:
fm.beginTransaction().replace(R.id.fragment_container,new SecondFragment()).commit();
break;
case 3:
fm.beginTransaction().replace(R.id.fragment_container,new ThirdFragment()).commit();
break;
}
mDrawerLayout.closeDrawer(mDrawer);
}
}
代码比较简单,就是找到控件,点击按钮切换不同的fragment,切换完成需要关闭DrawerLayout操作,没什么难点,只是有一个地方说一下,就是fragment之间的切换用fm.beginTransaction().replace(R.id.fragment_container,new FirstFragment()).commit();
实现,replace()
方法中第一个参数为装Fragment的容器id,第二个参数就是要切换到的fragment。
好了,以上就是对DrawerLayout的一个简单学习,在项目开发中还是很实用的。
网友评论