官方DrawerLayout的简单使用



官方DrawerLayout的简单使用

DrawerLayout是V4包下一种抽屉式布局效果!

简单效果图:


布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/dl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zhang.mydrawerlayout.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff0000">

        <Button
            android:id="@+id/bt_"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="点击弹出侧滑" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#1432da"></LinearLayout>

</android.support.v4.widget.DrawerLayout>

DrawerLayout是官方提供的,用起来比较简单,直接在布局就可实现侧滑

我这儿 android.support.v4.widget.DrawerLayout 下有两个子布局,第二个子布局为侧滑。

其实要使某个子布局作为侧滑,只需要在该布局下加上android:layout_gravity="left"即可,该属性所填方向便是侧滑 滑出的方向。

官方的DrawerLayout默认从边缘滑出。


java:

通过按钮点击 ,滑出侧滑视图


public class MainActivity extends AppCompatActivity {
    Button bt_;
    DrawerLayout dl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt_ = (Button) findViewById(R.id.bt_);
        dl = (DrawerLayout) findViewById(R.id.dl);

        bt_.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dl.openDrawer(Gravity.LEFT);
            }
        });
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值