Android側滑菜單DrawerLayout(抽屜布局)實現
應用場景:
由于側滑菜單有更好的用戶體驗效果,所以更多的App使用側滑抽屜式菜單列表,如網易客戶端、百度影音、愛奇藝等等。至此,側滑菜單有了更多的使用需求。
知識點介紹:
實現側滑菜單功能的方法有很多,如果開源的項目SlidingMenu,下載地址為https://github.com/jfeinstein10/SlidingMenu。該開源項目依賴于另一個開源項目ActionBarSherlock,下載地址為https://github.com/JakeWharton/ActionBarSherlock。這種實現方式,讀者可以平時有時間自行研究一下。本例主要介紹android-support-v4.jar提供的支持,android.support.v4.widget.DrawerLayout,可兼容低版本的系統。
使用方式:
第一步:新建測試項目DrawerLayout。
第二步:編寫相關的布局文件,主界面布局文件activity_main.xml。
第二步:編寫相關的布局文件,主界面布局文件activity_main.xml。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" >
<FrameLayout
android:id="@+id/fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="主內容界面" />
</FrameLayout>
<!--
android:layout_gravity="start" 側滑菜單在左邊
android:layout_gravity="start" 側滑菜單在右邊
-->
<LinearLayout
android:id="@+id/menu_layout"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="側拉菜單"
android:textColor="@android:color/black" />
<!--
android:cacheColorHint="#FFFFFF"
解決Android ListView 滾動 Item 背景變黑的問題
-->
<ListView
android:id="@+id/menu_listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#FFFFFF" >
</ListView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
【layout_first.xml】
【layout_next.xml】
ListView的ListItem的布局文件main_listitem.xml。
【main_listitem.xml】
【FirstFragment.java】
【MainActivity.java】
【MenuListViewAdapter.java】
import java.util.ArrayList; import java.util.HashMap; public class DataBuiltUtils { public static ArrayList<HashMap<String,String>> getMainMapList(){ ArrayList<HashMap<String, String>> tempMapList = new ArrayList<HashMap<String,String>>(); for(int i=0;i<5;i++){ HashMap<String, String> tempMap = new HashMap<String, String>(); tempMap.put("menu", "菜單【"+(i+1)+"】"); tempMapList.add(tempMap); } return tempMapList; } public static ArrayList<HashMap<String,String>> getFirstMapList(){ ArrayList<HashMap<String, String>> tempMapList = new ArrayList<HashMap<String,String>>(); for(int i=0;i<5;i++){ HashMap<String, String> tempMap = new HashMap<String, String>(); tempMap.put("menu", "FirstFragment菜單【"+(i+1)+"】"); tempMapList.add(tempMap); } return tempMapList; } }
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!