Android之側(cè)滑菜單DrawerLayout的使用介紹
在android support.v4 中有一個抽屜視圖控件DrawerLayout。使用這個控件,可以生成通過在屏幕上水平滑動打開或者關(guān)閉菜單,能給用戶一個不錯的體驗效果。
DrawerLayout分為側(cè)邊菜單和主內(nèi)容區(qū)兩部分,側(cè)邊菜單可以根據(jù)手勢展開與隱藏,主內(nèi)容區(qū)的部分可以隨著菜單的點擊而變化。DrawerLayout其實是一個控件,跟LinearLayout差不多,直接使用即可。
DrawerLayout屬性
1、drawerPosition:指定 drawer 將從屏幕的一側(cè)滑動。
2、drawerWidth :指定 drawer 的寬度,即從窗口的邊緣拉到視圖更精確的寬度。
3、keyboardDismissMode :確定鍵盤是否響應(yīng)拖動被駁回。
- 'none' (默認(rèn)值), 拖動不影響鍵盤。
- 'on-drag', 拖動開始,鍵盤被駁回。
4、onDrawerClose :導(dǎo)航視圖關(guān)閉時調(diào)用函數(shù)。
5、onDrawerOpen :導(dǎo)航視圖打開時調(diào)用函數(shù)。
6、onDrawerSlide :與導(dǎo)航視圖交互時調(diào)用函數(shù)。
7、onDrawerStateChanged :當(dāng) Drawer 狀態(tài)發(fā)生變化時調(diào)用函數(shù),drawer 有 3 種狀態(tài):
- idle -- 表示與導(dǎo)航視圖沒有交互
- dragging -- 表示目前有與導(dǎo)航視圖的交互
- settling -- 表示有與導(dǎo)航視圖的交互,并且導(dǎo)航視圖正在的關(guān)閉或打開。
8、renderNavigationView :導(dǎo)航圖將被渲染到屏幕的一側(cè),并且可以拉出。
案例
使用導(dǎo)入依賴庫
compile 'com.android.support:appcompat-v7:24.2.1'
布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/v4_drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/v4_drawerlayout_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/v4_text"
android:textSize="22sp"
android:textColor="@color/colorAccent"
android:gravity="center"
/>
</FrameLayout>
<ListView
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:id="@+id/v4_listview"
android:choiceMode="singleChoice"
android:background="@android:color/white" />
</android.support.v4.widget.DrawerLayout>
Activity
public class DrawerActivity extends AppCompatActivity {
private ListView listView;
private DrawerLayout drawerLayout;
private TextView textView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_activity);
initView();
}
private void initView()
{
listView=(ListView) findViewById(R.id.v4_listview);
drawerLayout=(DrawerLayout) findViewById(R.id.v4_drawerlayout);
textView=(TextView) findViewById(R.id.v4_text);
initDate();
}
private void initDate(){
final List<String> list = new ArrayList<String>();
list.add("網(wǎng)易");
list.add("騰訊");
list.add("新浪");
list.add("搜狐");
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textView.setText(list.get(position));
showDrawerLayout();
}
});
drawerLayout.openDrawer(Gravity.LEFT);//側(cè)滑打開 不設(shè)置則不會默認(rèn)打開
}
private void showDrawerLayout() {
if (!drawerLayout.isDrawerOpen(Gravity.LEFT)) {
drawerLayout.openDrawer(Gravity.LEFT);
} else {
drawerLayout.closeDrawer(Gravity.LEFT);
}
}
}
運行效果如圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用DrawerLayout實現(xiàn)仿QQ雙向側(cè)滑菜單
- Android原生側(cè)滑控件DrawerLayout使用方法詳解
- Android官方的側(cè)滑控件DrawerLayout的示例代碼
- Android中DrawerLayout實現(xiàn)側(cè)滑菜單效果
- Android DrawerLayout帶有側(cè)滑功能的布局類(1)
- Android側(cè)滑菜單控件DrawerLayout使用詳解
- Android組件DrawerLayout仿網(wǎng)易新聞v4.4側(cè)滑菜單
- android側(cè)滑菜單控件DrawerLayout使用方法詳解
- Android使用DrawerLayout實現(xiàn)側(cè)滑菜單效果
- Android布局控件DrawerLayout實現(xiàn)完美側(cè)滑效果
相關(guān)文章
Android Notification.Builder通知案例分享
這篇文章主要為大家分享了Android Notification.Builder通知案例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Android studio無法創(chuàng)建類和接口和提示問題的完美解決辦法
這篇文章主要介紹了Android studio無法創(chuàng)建類和接口和提示問題解決辦法,內(nèi)容比較簡單,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-04-04
Android實現(xiàn)點擊AlertDialog上按鈕時不關(guān)閉對話框的方法
這篇文章主要介紹了Android實現(xiàn)點擊AlertDialog上按鈕時不關(guān)閉對話框的方法,涉及設(shè)置監(jiān)聽的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-02-02
Spi機制在Android開發(fā)的應(yīng)用示例詳解
這篇文章主要為大家介紹了Spi機制在Android開發(fā)的應(yīng)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
Android編程實現(xiàn)屏幕自適應(yīng)方向尺寸與分辨率的方法
這篇文章主要介紹了Android編程實現(xiàn)屏幕自適應(yīng)方向尺寸與分辨率的方法,涉及Android屏幕分辨率、布局、橫豎屏切換等相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-12-12

