侧滑菜单--DrawerLayout的使用

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】从右向左滑出菜单。

说明和注意事项:

  1. DrawerLayout需为根节点;
  2. DrawerLayout内包含两个子viewGroup:第一个是主内容区域(必须放到第一个),第二个是侧滑菜单区域;
  3. 菜单区域的宽度建议设置为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;

        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值