DrawerLayout和SlidingMenu左侧滑右侧滑

DrawerLayout

布局

<androidx.drawerlayout.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/drawer_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar_id"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#FFEB3B">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">


                <ImageView
                    android:id="@+id/img_id"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:src="@mipmap/ic_launcher" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="戴鑫" />

                <ImageView
                    android:id="@+id/img_right"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:src="@mipmap/ic_launcher" />
            </RelativeLayout>
        </androidx.appcompat.widget.Toolbar>
    </LinearLayout>

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#F44336"
        android:orientation="vertical">

        <Button
            android:id="@+id/btn1_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="111" />

        <Button
            android:id="@+id/btn2_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="222" />
    </LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>

先去掉自带的ActionBar,在清单文件中

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

SlidingMenu实现抽屉

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
添加项目

Activity代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private DrawerLayout drawerId;
    private ImageView imgId;
    private Button btn1Id;
    private Button btn2Id;
    private ImageView imgRight;
    private Button btn3Id;
    private Button btn4Id;


    //侧滑抽屉
    private SlidingMenu slidingMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerId = (DrawerLayout) findViewById(R.id.drawer_id);
        imgId = (ImageView) findViewById(R.id.img_id);
        imgRight = (ImageView) findViewById(R.id.img_right);
        btn1Id = (Button) findViewById(R.id.btn1_id);
        btn2Id = (Button) findViewById(R.id.btn2_id);

        btn1Id.setOnClickListener(this);
        btn2Id.setOnClickListener(this);
        View inflate = LayoutInflater.from(this).inflate(R.layout.layout_slide, null);
        btn3Id = (Button) inflate.findViewById(R.id.btn3_id);
        btn4Id = (Button) inflate.findViewById(R.id.btn4_id);
        imgId.setOnClickListener(this);
        imgRight.setOnClickListener(this);
        //创建SlidingMenu对象
        slidingMenu = new SlidingMenu(this);
        //设置方向
        slidingMenu.setMode(SlidingMenu.RIGHT);
        //设置偏移量
        slidingMenu.setBehindOffset(300);
        //设置抽屉UI  传入View对象
        btn3Id.setOnClickListener(this);
        btn4Id.setOnClickListener(this);
        slidingMenu.setMenu(inflate);
        //设置指定的acitvity绑定
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //打开抽屉
            case R.id.img_id:
                drawerId.openDrawer(Gravity.LEFT);
                break;
            case R.id.btn1_id:
                Toast.makeText(this, "111", Toast.LENGTH_SHORT).show();
                drawerId.closeDrawer(Gravity.LEFT);
                break;
            case R.id.btn2_id:
                Toast.makeText(this, "111", Toast.LENGTH_SHORT).show();
                drawerId.closeDrawer(Gravity.LEFT);
                break;
            case R.id.img_right://打开slidingMenu抽屉
                slidingMenu.showMenu();
                break;
            case R.id.btn3_id:
                Toast.makeText(this, "333", Toast.LENGTH_SHORT).show();
                slidingMenu.showContent();//显示主界面
                break;
            case R.id.btn4_id:
                Toast.makeText(this, "444", Toast.LENGTH_SHORT).show();
                slidingMenu.showContent();
                break;
        }
    }
}

SlidingMenu子布局、

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn3_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="333" />
    <Button
        android:id="@+id/btn4_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="444" />
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值