Android使用DrawerLayout實(shí)現(xiàn)側(cè)滑菜單效果
一、概述
DrawerLayout是一個(gè)可以方便的實(shí)現(xiàn)Android側(cè)滑菜單的組件,我最近開發(fā)的項(xiàng)目中也有一個(gè)側(cè)滑菜單的功能,于是DrawerLayout就派上用場(chǎng)了。如果你從未使用過DrawerLayout,那么本篇博客將使用一個(gè)簡(jiǎn)單的案例帶你迅速掌握DrawerLayout的用法。
二、效果圖
三、代碼實(shí)現(xiàn)
主布局activity_main.xml
<?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" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:fitsSystemWindows="true" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/blueStatus" android:minHeight="?attr/actionBarSize" app:navigationIcon="?attr/homeAsUpIndicator" app:theme="@style/Theme.AppCompat.NoActionBar"> </android.support.v7.widget.Toolbar> <include layout="@layout/title_layout" /> <android.support.v4.widget.DrawerLayout xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:openDrawer="start"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:layout_width="260dp" android:layout_height="match_parent" android:layout_gravity="right"> <include layout="@layout/drawer_content" /> </LinearLayout> </android.support.v4.widget.DrawerLayout> </LinearLayout>
To use a DrawerLayout, position your primary content view as the first child with a width and height of match_parent. Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.
當(dāng)你使用DrawerLayout的時(shí)候,DrawerLayout的第一個(gè)元素就是主要內(nèi)容區(qū)域(在本案例中是ListView),它的寬高必須是match_parent。
在主要內(nèi)容區(qū)域的后面添加側(cè)滑視圖(在本案例中是drawer_content.xml),并且通過設(shè)置layout_gravity來決定它是左滑還是右滑,通常這個(gè)側(cè)滑視圖的高度設(shè)為match_parent。
drawer_content.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/background" android:orientation="vertical" android:padding="@dimen/activity_horizontal_margin"> <TextView style="@style/NormalTextView" android:layout_width="wrap_content" android:layout_height="40dp" android:text="船中文名" /> <EditText style="@style/SmallGreyTextView" android:layout_width="match_parent" android:layout_height="40dp" android:background="@drawable/btn_round_white" android:padding="@dimen/margin_8" /> <TextView style="@style/NormalTextView" android:layout_width="wrap_content" android:layout_height="40dp" android:text="船英文名" /> <EditText style="@style/SmallGreyTextView" android:layout_width="match_parent" android:layout_height="40dp" android:background="@drawable/btn_round_white" android:padding="@dimen/margin_8" /> <TextView style="@style/NormalTextView" android:layout_width="wrap_content" android:layout_height="40dp" android:text="呼號(hào)" /> <EditText style="@style/SmallGreyTextView" android:layout_width="match_parent" android:layout_height="40dp" android:background="@drawable/btn_round_white" android:padding="@dimen/margin_8" /> <TextView style="@style/NormalTextView" android:layout_width="wrap_content" android:layout_height="40dp" android:text="IMO" /> <EditText style="@style/SmallGreyTextView" android:layout_width="match_parent" android:layout_height="40dp" android:background="@drawable/btn_round_white" android:padding="@dimen/margin_8" /> <TextView style="@style/NormalTextView" android:layout_width="wrap_content" android:layout_height="40dp" android:text="MMSI" /> <EditText style="@style/SmallGreyTextView" android:layout_width="match_parent" android:layout_height="40dp" android:background="@drawable/btn_round_white" android:padding="@dimen/margin_8" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <Button android:id="@+id/btn_confirm" style="@style/NormalGreyTextView" android:layout_width="80dp" android:layout_height="36dp" android:layout_alignParentRight="true" android:layout_centerInParent="true" android:layout_gravity="center_vertical" android:background="@drawable/btn_round_red" android:gravity="center" android:text="查詢" android:textColor="@color/white" /> </RelativeLayout> </LinearLayout>
這個(gè)布局文件就是側(cè)滑視圖,如圖:
MainActivity.java
package com.leohan.drawerlayoutdemo; import android.os.Bundle; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Gravity; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import butterknife.ButterKnife; import butterknife.InjectView; import butterknife.OnClick; public class MainActivity extends AppCompatActivity { @InjectView(R.id.toolbar) Toolbar toolbar; @InjectView(R.id.tv_search) TextView tvSearch; @InjectView(R.id.listView) ListView listView; @InjectView(R.id.drawer_layout) DrawerLayout drawerLayout; private List data = new ArrayList(); private ShipRecordAdapter adapter = new ShipRecordAdapter(this, data); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.inject(this); setSupportActionBar(toolbar); listView.setAdapter(adapter); getData(); } @Override public void onBackPressed() { if (drawerLayout.isDrawerOpen(GravityCompat.START)) { drawerLayout.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } /** * 獲取類別數(shù)據(jù) */ private void getData() { for (int i = 0; i < 6; i++) { Map<String, Object> map = new HashMap<>(); data.add(map); } adapter.notifyDataSetChanged(); } @OnClick(R.id.tv_search) public void onClick(View view) { switch (view.getId()) { case R.id.tv_search: if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) { drawerLayout.closeDrawer(Gravity.RIGHT); } else { drawerLayout.openDrawer(Gravity.RIGHT); } break; } } }
由于這里的側(cè)滑視圖是從右側(cè)滑動(dòng)出現(xiàn)的,因此側(cè)滑視圖的layout_gravity設(shè)置為right或者end,如果是左滑就設(shè)置為left或者start。當(dāng)我們手動(dòng)控制側(cè)滑視圖的打開或者關(guān)閉的時(shí)候,需要執(zhí)行以下代碼:
//close drawerLayout.closeDrawer(Gravity.RIGHT); //open drawerLayout.openDrawer(Gravity.RIGHT);
至此DrawerLayout的基本使用就已經(jīng)掌握了,更深入的了解DrawerLayout,請(qǐng)參考Google官方文檔Creating a Navigation Drawer。
項(xiàng)目源碼:https://github.com/leoleohan/DrawerLayoutDemo
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android側(cè)滑菜單之DrawerLayout用法詳解
- Android自定義實(shí)現(xiàn)側(cè)滑菜單效果
- Android實(shí)現(xiàn)QQ側(cè)滑菜單效果
- Android側(cè)滑菜單控件DrawerLayout使用詳解
- android側(cè)滑菜單控件DrawerLayout使用方法詳解
- Android使用DrawerLayout實(shí)現(xiàn)雙向側(cè)滑菜單
- Android中DrawerLayout實(shí)現(xiàn)側(cè)滑菜單效果
- Android Drawerlayout實(shí)現(xiàn)側(cè)滑菜單效果
- android實(shí)現(xiàn)左右側(cè)滑菜單效果
- Android實(shí)現(xiàn)側(cè)滑菜單DrawerLayout
相關(guān)文章
Android 實(shí)現(xiàn)搶購倒計(jì)時(shí)功能的示例
這篇文章主要介紹了Android 實(shí)現(xiàn)搶購倒計(jì)時(shí)功能的示例,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下2021-03-03Android自定義指示器時(shí)間軸效果實(shí)例代碼詳解
指示器時(shí)間軸在外賣、購物類的APP里會(huì)經(jīng)常用到,效果大家都知道的差不多吧,下面小編通過實(shí)例代碼給大家分享Android自定義指示器時(shí)間軸效果,需要的朋友參考下吧2017-12-12Android實(shí)現(xiàn)的數(shù)字格式化用法示例
這篇文章主要介紹了Android實(shí)現(xiàn)的數(shù)字格式化用法,結(jié)合實(shí)例形式分析了Android數(shù)學(xué)運(yùn)算中數(shù)字格式化輸出的相關(guān)技巧,需要的朋友可以參考下2016-08-08Android自定義TextBanner實(shí)現(xiàn)自動(dòng)滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android自定義TextBanner實(shí)現(xiàn)自動(dòng)滾動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07Android仿淘寶頭條向上滾動(dòng)廣告條ViewFlipper
這篇文章主要為大家詳細(xì)介紹了Android仿淘寶頭條向上滾動(dòng)廣告條ViewFlipper,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Android應(yīng)用的LinearLayout中嵌套R(shí)elativeLayout的布局用法
這篇文章主要介紹了Android應(yīng)用的LinearLayout中嵌套R(shí)elativeLayout的布局用法,文后還給出了線性布局中一些組件位置的調(diào)試經(jīng)驗(yàn),需要的朋友可以參考下2016-04-04Flutter簡(jiǎn)潔實(shí)用的圖片編輯器的實(shí)現(xiàn)
本文主要介紹了Flutter簡(jiǎn)潔實(shí)用的圖片編輯器的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02android創(chuàng)建手勢(shì)識(shí)別示例代碼
使用一些瀏覽器或者輸入法應(yīng)用時(shí)會(huì)有一些手勢(shì)操作,還可以自定義手勢(shì)。這些神奇的操作是怎么做的呢?這一篇重點(diǎn)記錄手勢(shì)的識(shí)別和創(chuàng)建2014-01-01