DrawerLayout的使用
简介:DrawerLayout是【Support v4】包中实现了侧滑菜单效果的控件。DrawerLayout分为侧边菜单和主内容区两部分,侧边菜单可以根据手势展开与隐藏,主内容区的部分也可以随着菜单的点击而变化(需要自己实现)。
效果:
二、xml
示例:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start">
<!--第一部分:主内容区域 开始-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/theme_color">
<ImageView
android:id="@+id/show_drawer_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="标题"
android:textColor="#ffffff"
android:textSize="18sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/theme_blue">
<TextView
android:id="@+id/drawer_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="内容"
android:textColor="#ffffff"
android:textSize="100sp" />
</RelativeLayout>
</LinearLayout>
<!--第二部分;菜单区域 开始-->
<android.support.design.widget.NavigationView
android:id="@+id/drawer_menu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/theme_color">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="菜单"
android:textColor="#ffffff"
android:textSize="50sp" />
<TextView
android:id="@+id/drawer_menu_item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="item--1"
android:textColor="#ffffff"
android:textSize="18sp" />
<TextView
android:id="@+id/drawer_menu_item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="item--2"
android:textColor="#ffffff"
android:textSize="18sp" />
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
属性说明:
- android:layout_gravity: 【start】从左向右滑出菜单;【end】从右向左滑出菜单。
说明和注意事项:
- DrawerLayout需为根节点;
- DrawerLayout内包含两个子viewGroup:第一个是主内容区域(必须放到第一个),第二个是侧滑菜单区域;
- 菜单区域的宽度建议设置为240dp,最好不好超过360dp(为了能看到一些主内容区域)。
二、代码
public class DrawerLayoutActivity extends AppCompatActivity {
@BindView(R.id.drawer_layout)
DrawerLayout mDrawerLayout;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_layout);
ButterKnife.bind(this);
}
@OnClick({R.id.show_drawer_menu})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.show_drawer_menu:
mDrawerLayout.openDrawer(Gravity.START);
break;
}
}
}