Android Studio 2 - 9 ActionBar和DrawLayout和SlidingMenu

Android Studio 2 - 9 ActionBar和DrawLayout和SlidingMenu

DrawerLayout

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!--android:layout_gravity="left"-->

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/dDrawerLayout_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#4CAF50"
            android:orientation="vertical">

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="8"
                android:background="#4CAF50"></LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="4"
                android:background="#2196F3"></LinearLayout>

        </LinearLayout>

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

            <Button
                android:id="@+id/btn_closeD"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="关闭"
                android:textSize="25sp" />

            <Button
                android:id="@+id/btn_toastD"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="吐司"
                android:textSize="25sp" />
        </LinearLayout>

    </androidx.drawerlayout.widget.DrawerLayout>

</LinearLayout>

代码

package com.example.day09drawer;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView iv_image;
    private Button btn_closeD;
    private Button btn_toastD;
    private DrawerLayout dDrawerLayout_main;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        iv_image = (ImageView) findViewById(R.id.iv_image);
        btn_closeD = (Button) findViewById(R.id.btn_closeD);
        btn_toastD = (Button) findViewById(R.id.btn_toastD);
        dDrawerLayout_main = (DrawerLayout) findViewById(R.id.dDrawerLayout_main);

        btn_closeD.setOnClickListener(this);
        btn_toastD.setOnClickListener(this);
        iv_image.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_closeD:
                dDrawerLayout_main.closeDrawer(Gravity.LEFT);
                break;
            case R.id.btn_toastD:
                Toast.makeText(this, "么么达", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_image:
                dDrawerLayout_main.openDrawer(Gravity.LEFT);
                break;
            default:
        }
    }

}

SlidingMenu

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#4CAF50"
    tools:context=".SlideActivity">

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

</LinearLayout>

代码

package com.example.day09drawer;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

public class SlideActivity extends AppCompatActivity {

    private SlidingMenu slidingMenu;
    private ImageView iv_SlideImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slide);

        initView();

        initMenu();

    }

    private void initView() {
        iv_SlideImage = (ImageView) findViewById(R.id.iv_SlideImage);
        iv_SlideImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                slidingMenu.showMenu();//显示抽屉
            }
        });
    }

    private void initMenu() {

        //TODO 1:创建菜单对象
        slidingMenu = new SlidingMenu(this);
        //TODO 2:设置属性
        slidingMenu.setMode(SlidingMenu.LEFT);
        //TOUCHMODE_FULLSCREEN全屏;TOUCHMODE_MARGIN边界;TOUCHMODE_NONE不能滑动
        //在屏幕的任何位置左滑都可以显示出来
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        //单位是像素px 侧滑菜单滑出后,界面剩余的宽度。
        slidingMenu.setBehindOffset(200);
        //绑定到指定的Context
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        //TODO 3:设置布局并且相应布局上面的按钮
        View view = LayoutInflater.from(this).inflate(R.layout.slide, null);
        Button btn_slide_close = view.findViewById(R.id.btn_slide_close);
        Button btn_slide_Toast = view.findViewById(R.id.btn_slide_Toast);
        btn_slide_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭抽屉
                // slidingMenu.showMenu();//显示抽屉
                slidingMenu.showContent();//显示主页面
            }
        });
        //设置布局
        slidingMenu.setMenu(view);

        
        // TODO 4:设置监听listener,监听侧滑菜单的打开和关闭.
        slidingMenu.setOnOpenedListener(new SlidingMenu.OnOpenedListener() {
            @Override
            public void onOpened() {
//                Toast.makeText(SlideActivity.this, "打开了", Toast.LENGTH_SHORT).show();
            }
        });
        slidingMenu.setOnClosedListener(new SlidingMenu.OnClosedListener() {
            @Override
            public void onClosed() {
//                Toast.makeText(SlideActivity.this, "侧滑菜单关闭", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

ToolBar

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ToolBActivity">

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/dDrawerLayout_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#4CAF50"
            android:orientation="vertical">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/tToolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#fff"></androidx.appcompat.widget.Toolbar>

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

            <TextView
                android:id="@+id/tv_tool"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="文本"
                android:textColor="#FFEB3B"
                android:textSize="30sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="8"
                android:background="#4CAF50"></LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="4"
                android:background="#2196F3"></LinearLayout>

        </LinearLayout>

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

            <Button
                android:id="@+id/btn_tool_close"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="关闭"
                android:textSize="25sp" />

            <Button
                android:id="@+id/btn_tool_Toast"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="吐司"
                android:textSize="25sp" />
        </LinearLayout>

    </androidx.drawerlayout.widget.DrawerLayout>

</LinearLayout>

代码

//TODO ToolBar部分
        tToolbar = (Toolbar) findViewById(R.id.tToolbar);
        //加载菜单
        tToolbar.inflateMenu(R.menu.iitem);
        //事件监听
        tToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.one:
                        Toast.makeText(ToolBActivity.this, "1", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.two:
                        Toast.makeText(ToolBActivity.this, "22", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.three:
                        Toast.makeText(ToolBActivity.this, "333", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                }
                return true;
            }
        });
// slidingMenu.showMenu();//显示抽屉
                slidingMenu.showContent();//显示主页面
发布了20 篇原创文章 · 获赞 4 · 访问量 894

猜你喜欢

转载自blog.csdn.net/v1141261428/article/details/100675294