Android 侧滑栏 (DrawerLayout)
生活随笔
收集整理的這篇文章主要介紹了
Android 侧滑栏 (DrawerLayout)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
DrawerLayout 實現側滑欄非常簡單 支持左滑動以及右滑動 默認滑動出來側滑欄
DrawerLayout ?需要引入support:appcompat 庫,一般創建項目的時候自帶的有,這個庫就不用管了
DrawerLayout 需要使用 android.support.v4.widget.DrawerLayout 作為父類布局?
側滑欄的內容都是在2測所以需要使用的android:layout_gravity="start" 或者 android:layout_gravity="end"
start 使得側滑欄位于左側,end 使得側滑欄位于右側
側滑欄打開的方法
drawerLayout.openDrawer(Gravity.START); //打開左側側滑欄
drawerLayout.closeDrawer(Gravity.START); //關閉左側側滑欄
drawerLayout.openDrawer(Gravity.END);//代開右側側滑欄
drawerLayout.closeDrawer(Gravity.END);//關閉右側側滑欄
主布局代碼
<?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:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/drawer"tools:context=".MainActivity"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/left_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:text="左邊" /><Buttonandroid:id="@+id/right_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:text="右邊" /></RelativeLayout><includelayout="@layout/left_layout"/><includelayout="@layout/right_layout"/></android.support.v4.widget.DrawerLayout>
代碼使用了include 插入布局,另外在寫一個布局,這樣方便自己寫布局的時候查看布局樣式
left_layout 布局代碼
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="280dp"android:layout_height="match_parent"android:layout_gravity="start"android:background="#00d8a0"><Buttonandroid:id="@+id/left_child_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="左邊側滑欄內容"/></RelativeLayout>
這樣寫樣式,查看比較方便
right_layout 布局內容
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="280dp"android:layout_height="match_parent"android:layout_gravity="end"android:background="@color/colorPrimary"><Buttonandroid:id="@+id/right_child_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="右邊側滑欄內容" /></RelativeLayout>
?
代碼
package com.hly.drawerlayout;import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;public class MainActivity extends AppCompatActivity {private DrawerLayout drawerLayout;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);drawerLayout = findViewById(R.id.drawer);findViewById(R.id.left_btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {drawerLayout.openDrawer(Gravity.START);}});findViewById(R.id.left_child_btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {drawerLayout.closeDrawer(Gravity.START);}});findViewById(R.id.right_btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {drawerLayout.openDrawer(Gravity.END);}});findViewById(R.id.right_child_btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {drawerLayout.closeDrawer(Gravity.END);}});}
}
?
雖說代碼簡單,但是復習這里,我還是寫了一個demo 地址連接
總結
以上是生活随笔為你收集整理的Android 侧滑栏 (DrawerLayout)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RecyclerView 点击Item
- 下一篇: 输卵管不通原因