欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

DrawerLayout和SlidingMenu左侧滑右侧滑

程序员文章站 2022-03-12 19:43:41
DrawerLayout布局

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实现抽屉

DrawerLayout和SlidingMenu左侧滑右侧滑
DrawerLayout和SlidingMenu左侧滑右侧滑

DrawerLayout和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>

本文地址:https://blog.csdn.net/Sun_Sees_You/article/details/107522652

相关标签: android studio java