国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android使用DrawerLayout實現雙向側滑菜單

2019-12-12 01:35:21
字體:
來源:轉載
供稿:網友

前言

  在android開發中,很多的app都有使用側滑菜單,有的是自定義控件來實現側滑菜單,但是android給我們提供了DrawerLayout類來實現側滑菜單,側滑效果很好,今天我就說說怎么去使用它來實現側滑菜單。

實現

  我們先來看一下效果圖:

這里我們實現的雙向側滑菜單,在界面上部加入了兩個按鈕,點擊就會打開菜單或者關閉菜單,當然也可以自己去滑動。

布局文件的代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout  android:layout_width="fill_parent" android:layout_height="45dp" android:background="#00ff00" > <Button  android:id="@+id/btn_toggle_left" android:layout_width="wrap_content" android:layout_height="50dp" android:text="左開關" /> <Button  android:layout_alignParentRight="true" android:id="@+id/btn_toggle_right" android:layout_width="wrap_content" android:layout_height="50dp" android:text="右開關" /> </RelativeLayout> <android.support.v4.widget.DrawerLayout android:id="@+id/drawerlayout" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 主布局,位于DrawerLayout的第一次子控件,位置不可以放錯 --> <FrameLayout  android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView  android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/mainbackground" /> </FrameLayout> <!-- 左側菜單 --> <RelativeLayout android:id="@+id/layout_menu_left" android:layout_gravity="start"  android:layout_width="150dp" android:layout_height="fill_parent" android:background="#000" > <TextView android:textColor="#ffffff" android:gravity="center" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="左側菜單" /> </RelativeLayout> <!-- 右側菜單 --> <RelativeLayout android:id="@+id/layout_menu_right" android:layout_gravity="end"  android:layout_width="150dp" android:layout_height="fill_parent" android:background="#000" > <TextView android:textColor="#ffffff" android:gravity="center" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="右側菜單" /> </RelativeLayout> </android.support.v4.widget.DrawerLayout></LinearLayout>

MainActivity的代碼:

public class MainActivity extends Activity implements OnClickListener{ private DrawerLayout mDrawerLayout; private View v_menu_left,v_menu_right; private Button btn_left,btn_right; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initListener(); } private void initListener() { btn_left.setOnClickListener(this); btn_right.setOnClickListener(this); } private void initView() { mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout); v_menu_left = findViewById(R.id.layout_menu_left); v_menu_right = findViewById(R.id.layout_menu_right); btn_left = (Button) findViewById(R.id.btn_toggle_left); btn_right = (Button) findViewById(R.id.btn_toggle_right); } @Override public void onClick(View v) { if(v.getId()==R.id.btn_toggle_left){ toggleLeft(); }else if(v.getId()==R.id.btn_toggle_right){ toggleRight(); } } private void toggleRight() { if(mDrawerLayout.isDrawerOpen(v_menu_right)){ mDrawerLayout.closeDrawer(v_menu_right); }else{ mDrawerLayout.openDrawer(v_menu_right); } } private void toggleLeft() { if(mDrawerLayout.isDrawerOpen(v_menu_left)){ mDrawerLayout.closeDrawer(v_menu_left); }else{ mDrawerLayout.openDrawer(v_menu_left); } }}

  在布局文件中,第一個子控件是主布局,就是顯示在界面中央的位置,然后第二個和第三個控件作為左菜單和右菜單在兩側隱藏,然后滑動的時候慢慢顯示出來。在第二和第三個控件的屬性設置里,需要注意的是android:layout_gravity屬性,這個屬性決定了菜單的位置是左還是右。當設置成“start”的時候,菜單位于左側,當設置成“end”的時候,菜單位于右側,所以菜單的位置和控件的順序沒有關系,只和屬性值有關。

  然后在MainActivity里面,我們得到DrawerLayout 對象,和兩個菜單對象,對按鈕添加點擊方法。拿左菜單來說,當點擊按鈕的時候,如果左菜單是關閉的,那么我們就打開菜單,如果菜單是打開的,那么我們就關閉它。這就需要知道DrawerLayout的幾個常用方法了。

isDrawerOpen(View v)

該方法用來判斷菜單是否處于打開狀態,傳入的是一個View,表示菜單的View,也就是左菜單或者是右菜單。因為菜單的數量有一個或者以上,所以需要傳入不同的View來判斷是哪一個菜單。

closeDrawer(View v)

該方法用來關閉菜單,傳入的也是菜單的View

openDrawer(View v)

該方法用來打開菜單,同關閉菜單的操作相似。

  用這三個方法基本就可以實現上面的效果了,好了,簡單的雙向側滑菜單就完成了,不需要使用自定義的控件,自定義的控件可能有更加豐富的動畫效果,這就需要大家自己去是實現了。

源碼下載點這里

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 威海市| 钟祥市| 临朐县| 龙山县| 渭南市| 林口县| 微博| 新疆| 玉林市| 瓦房店市| 延寿县| 海门市| 南川市| 克东县| 牟定县| 赞皇县| 吴堡县| 曲阳县| 曲靖市| 华蓥市| 富锦市| 英山县| 榆社县| 周宁县| 铁力市| 孟村| 板桥市| 高阳县| 六枝特区| 建水县| 荃湾区| 额济纳旗| 武威市| 岳普湖县| 宁河县| 西乡县| 韶山市| 龙井市| 石阡县| 阿克苏市| 库车县|