在android support.v4 中有一個抽屜視圖控件DrawerLayout。使用這個控件,可以生成通過在屏幕上水平滑動打開或者關(guān)閉菜單,能給用戶一個不錯的體驗(yàn)效果。
最近在項(xiàng)目中,設(shè)計中有用到這個效果,但是是左右兩邊都能劃出這樣的一個菜單效果。經(jīng)過使用發(fā)現(xiàn),在xml布局中和代碼中,幾乎是添加添加同樣的代碼,就可以實(shí)現(xiàn)這種作用兩種菜單的效果。
效果圖如下:
左邊拉出菜單:
右邊拉出菜單效果:
具體的實(shí)現(xiàn)方法如下,結(jié)合代碼文件,跟大家分享一下:
主頁布局文件:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main_drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" > <RelativeLayout android:id="@+id/main_content_frame_parent" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" > <!-- 下層顯示的主要內(nèi)容 --> <FrameLayout android:id="@+id/main_content_frame" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:scrollbars="vertical" > </FrameLayout> </RelativeLayout> <!-- 左側(cè)滑動欄 --> <RelativeLayout android:id="@+id/main_left_drawer_layout" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="@android:color/transparent" android:paddingTop="50dp" > </RelativeLayout> <!-- 右側(cè)滑動欄 --> <RelativeLayout android:id="@+id/main_right_drawer_layout" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="end" android:background="@android:color/transparent" android:paddingTop="50dp" > </RelativeLayout> </android.support.v4.widget.DrawerLayout>
如上,使用DrawerLayout需要在布局文件跟目錄中引用,v4包中的DrawerLayout標(biāo)簽,并且寬和高,都設(shè)置為match_parent.里面framelayout用來現(xiàn)實(shí)菜單收起時,下層頁面的布局。
而main_left_drawer_layout和main_right_drawer_layout為左右兩個抽屜菜單對應(yīng)的父layout,需要注意的是,在DrawerLayout中,從左側(cè)開始使用android:layout_gravity="start",從右側(cè)開始,使用 android:layout_gravity="end"。
b.主布局代碼文件:
package com.demo.drawlayout; import android.os.Bundle; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v4.widget.DrawerLayout; import android.view.View; import android.widget.RelativeLayout; import android.widget.TextView; public class MainFrameLayout extends FragmentActivity { // 抽屜菜單對象 private ActionBarDrawerToggle drawerbar; public DrawerLayout drawerLayout; private TestFragment testFragment; private RelativeLayout left_menu_layout, right_xiaoxi_layout; private TextView text; @Override protected void onCreate(Bundle arg0) { super.onCreate(arg0); setContentView(R.layout.main_frame_activity); initView(); initEvent(); } public void initView(){ testFragment = new TestFragment(); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction f_transaction = fragmentManager.beginTransaction(); f_transaction.replace(R.id.main_content_frame_parent, testFragment); f_transaction.commitAllowingStateLoss(); initLeftLayout(); initRightLayout(); } public void initLeftLayout(){ drawerLayout = (DrawerLayout) findViewById(R.id.main_drawer_layout); //設(shè)置透明 drawerLayout.setScrimColor(0x00000000); //左邊菜單 left_menu_layout = (RelativeLayout) findViewById(R.id.main_left_drawer_layout); View view2 = getLayoutInflater().inflate(R.layout.menu_layout, null); text=(TextView)view2.findViewById(R.id.text); text.setText("左邊測試菜單"); left_menu_layout.addView(view2); } public void initRightLayout(){ //左邊菜單 right_xiaoxi_layout = (RelativeLayout) findViewById(R.id.main_right_drawer_layout); View view = getLayoutInflater().inflate(R.layout.xiaoxi_layout, null); text=(TextView)view.findViewById(R.id.text); text.setText("右邊測試菜單"); right_xiaoxi_layout.addView(view); } private void initEvent() { drawerbar = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_launcher, R.string.open, R.string.close) { //菜單打開 @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); } // 菜單關(guān)閉 @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); } }; drawerLayout.setDrawerListener(drawerbar); } //左邊菜單開關(guān)事件 public void openLeftLayout() { if (drawerLayout.isDrawerOpen(left_menu_layout)) { drawerLayout.closeDrawer(left_menu_layout); } else { drawerLayout.openDrawer(left_menu_layout); } } // 右邊菜單開關(guān)事件 public void openRightLayout() { if (drawerLayout.isDrawerOpen(right_xiaoxi_layout)) { drawerLayout.closeDrawer(right_xiaoxi_layout); } else { drawerLayout.openDrawer(right_xiaoxi_layout); } } }
代碼很簡單,相應(yīng)的地方都有注釋。這里就不羅嗦了。
主要說一下:抽屜菜單的開關(guān)事件就是,把抽屜視圖添加到ActionBarDrawerToggle開關(guān)中,通關(guān)它的開關(guān)事件來控制菜單的打開和關(guān)閉,同樣,一個菜單需要注冊一個事件,兩個菜單,也是把菜單加到這個ActionBarDrawerToggle 中進(jìn)行管理。它會自行處理左右兩個菜單的打開和關(guān)閉,而不會出現(xiàn)同時打開的現(xiàn)象,這一點(diǎn)這個控件設(shè)計的還是挺棒的。
余下的工作,就是大家根據(jù)自己的需要,自己寫左右菜單里面的內(nèi)容和事件了。另外,以前看到一個帖子說,在DrawerLayout中使用listview,listview會無效,這個說法好像是不成立的,至少,在我們的項(xiàng)目中,菜單中添加listview或者其他常用控件,點(diǎn)擊事件都不會受到影響。
相關(guān)的代碼我添加在了附件中,感興趣的朋友,可以下載互相學(xué)習(xí)一下。
附件:http://down.51cto.com/data/2364744另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享文章:Android使用DrawerLayout創(chuàng)建左右兩個抽屜菜單-創(chuàng)新互聯(lián)
文章源于:http://jinyejixie.com/article2/hegic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、軟件開發(fā)、動態(tài)網(wǎng)站、定制網(wǎng)站、搜索引擎優(yōu)化、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容