国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Android實現(xiàn)側(cè)滑菜單DrawerLayout

瀏覽:88日期:2022-09-24 09:40:34

本文實例為大家分享了Android實現(xiàn)側(cè)滑菜單的具體代碼,供大家參考,具體內(nèi)容如下

點擊左側(cè)滑動

效果如下

Android實現(xiàn)側(cè)滑菜單DrawerLayout

代碼實現(xiàn)過程:

1.導(dǎo)入框架build.gradle中

//materialDesignimplementation ’com.google.android.material:material:1.0.0’

2.xml文件

主要的界面放在DrawerLayout 中,需要強調(diào)的是側(cè)滑菜單也就是下圖顯示的TextView一定要設(shè)置layout_gravity屬性,我是從左側(cè)滑動的,所以設(shè)置為start

<androidx.drawerlayout.widget.DrawerLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' android: android:fitsSystemWindows='true' tools:context='.MainActivity'> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width='match_parent' android:layout_height='match_parent'> <androidx.appcompat.widget.Toolbar android: android:layout_width='match_parent' android:layout_height='?attr/actionBarSize' android:background='@color/colorPrimary' app:layout_constraintTop_toTopOf='parent' app:popupTheme='@style/ThemeOverlay.AppCompat.Light' /> </androidx.constraintlayout.widget.ConstraintLayout> <TextView android:layout_width='match_parent' android:layout_height='match_parent' android:layout_gravity='start' android:background='#FFFFFF' android:fitsSystemWindows='true' android:text='i am a textView' /></androidx.drawerlayout.widget.DrawerLayout>

3.MainActivity

綁定xml文件中的toobar

protected void setupToobar() { toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); if (null != getSupportActionBar()) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); } }

MainActivity 中將點擊之后觸發(fā)側(cè)邊滑動的圖片ic_menu動態(tài)放到toolbr中

@Override protected void setupViews() { setupToobar(); drawerLayout = findViewById(R.id.drawer); if (null != getSupportActionBar()) { getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu); } }

android.R.id.home 觸發(fā)左側(cè)滑動

@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: drawerLayout.openDrawer(GravityCompat.START); break; case R.id.item_search: Toast.makeText(MainActivity.this, '搜索', Toast.LENGTH_SHORT).show(); } return true; }

到這就結(jié)束了。

4.后話

可以在主內(nèi)容區(qū)里面再放一個布局,里面放各個fragment,就可以實現(xiàn)每個頁面都有側(cè)滑菜單的效果。側(cè)滑菜單里面的布局可以新建一個xml文件,然后include,可以看起來舒服點吧。其他的效果后面慢慢來吧。github下載地址

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Android
相關(guān)文章:
主站蜘蛛池模板: 黄陵县| 梨树县| 温州市| 洱源县| 大悟县| 日照市| 瑞丽市| 诏安县| 拉萨市| 鹰潭市| 本溪市| 利津县| 苏尼特左旗| 阜康市| 太原市| 集安市| 泰来县| 巴彦淖尔市| 河池市| 石棉县| 遂宁市| 永济市| 法库县| 安多县| 新乡市| 临江市| 岑溪市| 襄垣县| 徐州市| 库车县| 鸡东县| 汉寿县| 长顺县| 彭水| 徐汇区| 绥棱县| 长乐市| 菏泽市| 大丰市| 靖州| 新乡市|