android 側邊選單做法Navigation Drawer
側邊選單的關鍵字為DrawerLayout,在網路上搜尋了一些資料。
首先頁面必須使用這個容器裝
1.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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/drawerlayout" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<LinearLayout | |
android:id="@+id/nav_view" | |
android:orientation="horizontal" | |
android:layout_width="250dp" | |
android:layout_height="match_parent" | |
android:layout_gravity="left"> | |
<include | |
layout="@layout/nav_header_main" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_weight="1" /> | |
</LinearLayout> | |
</android.support.v4.widget.DrawerLayout> |
之後在裡面新增一個id為nav_view的容器,
這邊的容器應該可以隨意,
nav_view能不能夠修改還沒有測試,
這邊應該是參考android新增專案的範例改的。
2.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private DrawerLayout mDrawerLayout; | |
private ActionBarDrawerToggle mToggle; | |
mDrawerLayout=(DrawerLayout)findViewById(R.id.drawerlayout); | |
mToggle=new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);//必須用字串資源檔 | |
mDrawerLayout.addDrawerListener(mToggle);//監聽選單按鈕是否被觸擊 | |
mToggle.syncState();//隱藏顯示箭頭返回 | |
//讓 ActionBar 中的返回箭號置換成 Drawer 的三條線圖示。並且把這個觸發器指定給 layDrawer 。 | |
getSupportActionBar().setDisplayHomeAsUpEnabled(true); |
裡面include的xml
內容可以隨意
之後在mainActivity加入
完成!
留言
張貼留言