Android 抽屜效果的導(dǎo)航菜單實(shí)現(xiàn)代碼實(shí)例
看了很多應(yīng)用,覺(jué)得這種側(cè)滑的抽屜效果的菜單很好。
不用切換到另一個(gè)頁(yè)面,也不用去按菜單的硬件按鈕,直接在界面上一個(gè)按鈕點(diǎn)擊,菜單就滑出來(lái),而且感覺(jué)能放很多東西。
關(guān)于實(shí)現(xiàn),搜索了一下,有如下兩種:
1.用SlidingDrawer:http://developer.android.com/reference/android/widget/SlidingDrawer.html
但是不知道為什么這個(gè)類官方不建議再繼續(xù)用了:Deprecated since API level 17
2.用DrawerLayout:http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
Guide在這里:http://developer.android.com/training/implementing-navigation/nav-drawer.html
庫(kù)的引用
首先, DrawerLayout這個(gè)類是在Support Library里的,需要加上android-support-v4.jar這個(gè)包。
然后程序中用時(shí)在前面導(dǎo)入import android.support.v4.widget.DrawerLayout;
如果找不到這個(gè)類,首先用SDK Manager更新一下Android Support Library,然后在Android SDK\extras\android\support\v4路徑下找到android-support-v4.jar,復(fù)制到項(xiàng)目的libs路徑,將其Add to Build Path.
代碼1
布局:
<RelativeLayout 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.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- The main content view --> <!-- main content must be the first element of DrawerLayout because it will be drawn first and drawer must be on top of it --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- The navigation drawer --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="left" android:background="#111" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" /> </android.support.v4.widget.DrawerLayout> </RelativeLayout>
DrawerLayout的第一個(gè)子元素是主要內(nèi)容,即抽屜沒(méi)有打開(kāi)時(shí)顯示的布局。這里采用了一個(gè)FrameLayout,里面什么也沒(méi)放。
DrawerLayout的第二個(gè)子元素是抽屜中的內(nèi)容,即抽屜布局,這里采用了一個(gè)ListView。
主要的Activity(從官方實(shí)例中扒出來(lái)的):
package com.example.hellodrawer; import android.os.Bundle; import android.app.Activity; import android.content.res.Configuration; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; public class HelloDrawerActivity extends Activity { private String[] mPlanetTitles; private DrawerLayout mDrawerLayout; private ActionBarDrawerToggle mDrawerToggle; private ListView mDrawerList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello_drawer); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); // init the ListView and Adapter, nothing new initListView(); // set a custom shadow that overlays the main content when the drawer // opens mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) { /** Called when a drawer has settled in a completely closed state. */ public void onDrawerClosed(View view) { invalidateOptionsMenu(); // creates call to // onPrepareOptionsMenu() } /** Called when a drawer has settled in a completely open state. */ public void onDrawerOpened(View drawerView) { invalidateOptionsMenu(); // creates call to // onPrepareOptionsMenu() } }; // Set the drawer toggle as the DrawerListener mDrawerLayout.setDrawerListener(mDrawerToggle); // enable ActionBar app icon to behave as action to toggle nav drawer getActionBar().setDisplayHomeAsUpEnabled(true); // getActionBar().setHomeButtonEnabled(true); // Note: getActionBar() Added in API level 11 } private void initListView() { mDrawerList = (ListView) findViewById(R.id.left_drawer); mPlanetTitles = getResources().getStringArray(R.array.planets_array); // Set the adapter for the list view mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, mPlanetTitles)); // Set the list's click listener mDrawerList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // Highlight the selected item, update the title, and close the // drawer mDrawerList.setItemChecked(position, true); setTitle(mPlanetTitles[position]); mDrawerLayout.closeDrawer(mDrawerList); } }); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Pass the event to ActionBarDrawerToggle, if it returns // true, then it has handled the app icon touch event if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } // Handle your other action bar items... return super.onOptionsItemSelected(item); } }
比較糾結(jié)的是用了Level 11的一個(gè)API,這樣minSdkVersion就有限制,不能太低。
圖片資源Android官網(wǎng)示例處提供下載了。
程序運(yùn)行后效果如下:
抽屜打開(kāi)前:
抽屜打開(kāi)后:
代碼2
今天又看了一下DrawerLayout的類,發(fā)現(xiàn)有很多方法可以直接用的。
重新試了一下,其實(shí)不用上面那么麻煩,隨便自己定義一個(gè)按鈕控制抽屜的打開(kāi)就行:
布局:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".DrawerActivity" > <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- The main content view --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="open" /> </FrameLayout> <!-- The navigation drawer --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#111" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" /> </android.support.v4.widget.DrawerLayout> </RelativeLayout>
主要代碼:
package com.example.hellodrawer; import android.os.Bundle; import android.app.Activity; import android.support.v4.widget.DrawerLayout; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class DrawerActivity extends Activity { private DrawerLayout mDrawerLayout = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_drawer); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); Button button = (Button) findViewById(R.id.btn); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 按鈕按下,將抽屜打開(kāi) mDrawerLayout.openDrawer(Gravity.LEFT); } }); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Studio無(wú)法改變Button背景顏色解決辦法
今天我來(lái)和大家探討一個(gè)在Android開(kāi)發(fā)中常見(jiàn)但可能讓初學(xué)者感到困惑的問(wèn)題,如何在Android Studio中改變Button的背景顏色,這個(gè)問(wèn)題看似簡(jiǎn)單,但實(shí)際操作中可能會(huì)遇到一些意想不到的挑戰(zhàn),接下來(lái),我將從多個(gè)角度為大家提供解決方案,需要的朋友可以參考下2024-05-05Android中Service和Activity相互通信示例代碼
在android中Activity負(fù)責(zé)前臺(tái)界面展示,service負(fù)責(zé)后臺(tái)的需要長(zhǎng)期運(yùn)行的任務(wù)。下面這篇文章主要給大家介紹了關(guān)于Android中Service和Activity相互通信的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-09-09android實(shí)現(xiàn)App活動(dòng)定時(shí)自動(dòng)跳轉(zhuǎn)效果
本篇文章主要介紹了android實(shí)現(xiàn)App活動(dòng)定時(shí)自動(dòng)跳轉(zhuǎn)效果,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02Android獲取系統(tǒng)儲(chǔ)存以及內(nèi)存信息的方法(一)
這篇文章主要為大家詳細(xì)介紹了Android獲取系統(tǒng)儲(chǔ)存以及內(nèi)存信息的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android提高之BroadcastReceiver實(shí)例詳解
這篇文章主要介紹了Android的BroadcastReceiver用法,在Android的項(xiàng)目開(kāi)發(fā)中是比較實(shí)用的功能,需要的朋友可以參考下2014-08-08Kotlin中Lambda表達(dá)式與高階函數(shù)使用分析講解
lambda 本質(zhì)上是可以傳遞給函數(shù)的一小段代碼,Kotlin 與 Java 中的 Lambda 有一定的區(qū)別,除了對(duì) lambda 的全面支持外,還有內(nèi)聯(lián)函數(shù)等簡(jiǎn)潔高效的特性。下面我們來(lái)仔細(xì)看一下2022-12-12Android開(kāi)發(fā)人臉識(shí)別統(tǒng)計(jì)人臉數(shù)
這篇文章主要介紹了Android開(kāi)發(fā)人臉識(shí)別統(tǒng)計(jì)人臉數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10Android中EditText光標(biāo)在4.0中的bug及解決方法
這篇文章主要介紹了Android中EditText光標(biāo)在4.0中的bug及解決方法,簡(jiǎn)單分析了Android4.0版本中EditText光標(biāo)消息的原因及相應(yīng)的解決方法,需要的朋友可以參考下2016-01-01Android獲取手機(jī)通訊錄、sim卡聯(lián)系人及調(diào)用撥號(hào)界面方法
這篇文章主要介紹了Android獲取手機(jī)通訊錄、sim卡聯(lián)系人及調(diào)用撥號(hào)界面方法,本文分別給出實(shí)現(xiàn)代碼實(shí)現(xiàn)獲取通訊錄和sim卡的聯(lián)系人,以及權(quán)限配置和調(diào)用系統(tǒng)撥打電話的界面的實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04Android中使用orc實(shí)現(xiàn)文字識(shí)別實(shí)例
這篇文章主要介紹了Android中使用orc實(shí)現(xiàn)文字識(shí)別實(shí)例,詳細(xì)的介紹了orc的簡(jiǎn)介和用法,有興趣的可以了解一下2017-05-05