android側滑菜單筆記

來源:互聯網
上載者:User

標籤:

一、SlidingPaneLayout

  v4包下的控制項,使用簡單,功能簡潔。官方文檔明確說明該控制項只能左側滑動。使用如下:

<android.support.v4.widget.SlidingPaneLayout    android:id="@+id/slidingPaneLayout"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ListView        android:layout_width="200dp"        android:layout_height="match_parent"/>    <include layout="@layout/layout_main_content"/></android.support.v4.widget.SlidingPaneLayout>

  SlidingPaneLayout有兩個子節點: 第一個為功能表項目布局,第二個為內容布局。

  java代碼中找到該控制項,設定監聽:

  

SlidingPaneLayout spl = (SlidingPaneLayout) findViewById(R.id.slidingPaneLayout);if (spl != null) {    spl.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() {        @Override        public void onPanelSlide(View panel, float slideOffset) {            //正在滑動的監聽        }        @Override        public void onPanelOpened(View panel) {            //開啟的監聽        }        @Override        public void onPanelClosed(View panel) {            //關閉的監聽        }    });}

 

二 DrawerLayout(抽屜效果)

  

android.support.v4.widget.DrawerLayout    android:id="@+id/dl"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#ff00ff"        android:gravity="start"        android:text="主布局內容"/>    <TextView        android:layout_width="200dp"        android:layout_height="match_parent"        android:layout_gravity="start"        android:background="#00ffff"        android:text="側邊功能表列內容"/></android.support.v4.widget.DrawerLayout>

    Drawerlayout標籤中可以有三個子節點,一個是左邊菜單,一個是主布局,還有右側菜單,另外需要在左邊菜單起始位置設定為                   android:layout_gravity="start",否則會報錯

    注意:官方手冊提示:內容控制項必須寫在第一個,並且不能含有layout_gravity屬性

    範例程式碼如下:

private DrawerLayout dl;private Toolbar toolbar;private ActionBarDrawerToggle actionBarDrawerToggle;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    toolbar = (Toolbar) findViewById(R.id.toolbar);    setSupportActionBar(toolbar);//設定標題為當前的Toolbar    getSupportActionBar().setHomeButtonEnabled(true);//設定home按鈕可用    getSupportActionBar().setDisplayHomeAsUpEnabled(true);//設定顯示回到首頁的按鈕可用,即:返回箭頭顯示出來    dl = (DrawerLayout) findViewById(R.id.dl);//找到抽屜控制項DrawerLayout    //建立一個標題列與抽屜的關聯開關actionBarDrawerToggle = new ActionBarDrawerToggle(this, dl, toolbar, R.string.app_name, R.string.app_name);    actionBarDrawerToggle.syncState();//同步抽屜的指標狀態,只有加這一句抽屜的開關才會改變Toolbar上的NavigationIcon表徵圖    dl.addDrawerListener(actionBarDrawerToggle);//添加抽屜監聽者,該監聽者內部控制了ActionBar的NavigationIcon表徵圖按鈕    dl.addDrawerListener(new DrawerLayout.DrawerListener() {//添加開發人員自己處理的監聽者        @Override        public void onDrawerSlide(View drawerView, float slideOffset) {            System.out.println("----onDrawerSlide-  " + slideOffset);        }        @Override        public void onDrawerOpened(View drawerView) {            System.out.println("----onDrawerOpened-");        }        @Override        public void onDrawerClosed(View drawerView) {            System.out.println("----onDrawerClosed-");        }        @Override        public void onDrawerStateChanged(int newState) {            System.out.println("----onDrawerStateChanged-  " + newState);        }    });}

 

三、NavigationView

  MD的控制項,用於解決DrawerLayout中的功能表項目設定太麻煩的問題,主流的做法都是用NavigationView來做DrawerLayout的菜單。

  

      用於側滑菜單中的menu布局。之前Google在V4包中推出自己的 DrawerLayout作為抽屜側滑菜單,標準使用方法可以參考 google 原生態 抽屜式側滑菜單 Android DrawerLayout 布局的使用介紹。 
當時的官方布局是這樣的:

<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 -->      <FrameLayout          android:id="@+id/frame_content"          android:layout_width="match_parent"          android:layout_height="match_parent" >      </FrameLayout>       <ListView          android:id="@+id/drawer_list"          android:layout_width="240dp"          android:layout_height="match_parent"          android:layout_gravity="start"          android:background="#9999cc"          android:choiceMode="singleChoice"          android:divider="@android:color/transparent"          android:dividerHeight="0dp" >      </ListView>  </android.support.v4.widget.DrawerLayout>  

   

    其實這次Google只是將上面的ListView布局替換成NavigationView了。簡化了之前ListView寫適配器的繁瑣。

    先如今布局改成如下:

  

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/drawer_layout"    android:layout_width="match_parent"    android:layout_height="match_parent">
<!-- the main content view --> <include layout="@layout/layout_content" />
<!-- the navigetion view --> <android.support.design.widget.NavigationView android:id="@+id/navigationView" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="left" android:fitsSystemWindows="true" app:headerLayout="@layout/layout_header" app:menu="@layout/layout_menu"> </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

       

其中NavigationView 中的 android:layout_gravity=”start” 屬性來控制抽屜菜單從哪邊滑出,一般“start ”從左邊滑出,“end”從右邊滑出。

這裡最主要的兩個屬性分別是: 
1.app:headerLayout: 給NavigationView添加頭部布局 
2.app:menu:給NavigationView添加menu菜單布局

 

  代碼中控制NavigationView

  

private void initNavigationView(){        navigationView = (NavigationView) findViewById(R.id.navigationView);        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);        //設定側滑菜單選擇監聽事件        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {            @Override            public boolean onNavigationItemSelected(MenuItem menuItem) {                menuItem.setChecked(true);                //關閉抽屜側滑菜單                drawerLayout.closeDrawers();                return true;            }        });    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        if (item.getItemId() == android.R.id.home){            //開啟抽屜側滑菜單            drawerLayout.openDrawer(GravityCompat.START);        }        return super.onOptionsItemSelected(item);    }

  

      

關於NavigationView中item的字型顏色和icon選中狀態顏色是去當前主題theme中的

<--正常狀態下字型顏色和icon顏色-->

<item name="android:textColorPrimary">@android:color/darker_gray</item>

<--選中狀態icon的顏色和字型顏色-->

 <item name="colorPrimary">@color/accent_material_light</item>

 

      也可以通過如下方法或者屬性來改變這一狀態:

  1. setItemBackgroundResource(int):給menu設定背景資源,對應的屬性app:itemBackground
  2. setItemIconTintList(ColorStateList):給menu的icon設定顏色,對應的屬性app:itemIconTint
  3. setItemTextColor(ColorStateList):給menu的item設定字型顏色,對應的屬性app:itemTextColor

android側滑菜單筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.