最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年05月21日 12:02:45   作者:qq_36914492  
這篇文章主要為大家詳細介紹了Android實現(xiàn)側滑菜單DrawerLayout,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

點擊左側滑動

效果如下

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

1.導入框架build.gradle中

//materialDesign
implementation 'com.google.android.material:material:1.0.0'

2.xml文件

主要的界面放在DrawerLayout 中,需要強調的是側滑菜單也就是下圖顯示的TextView一定要設置layout_gravity屬性,我是從左側滑動的,所以設置為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:id="@+id/drawer"
 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:id="@+id/toolbar"
   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ā)側邊滑動的圖片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ā)左側滑動

@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;
 }

到這就結束了。

4.后話

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

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

相關文章

最新評論

积石山| 都安| 崇文区| 鄄城县| 罗山县| 蒙自县| 黄山市| 宽甸| 西华县| 宜兰县| 金山区| 永州市| 临漳县| 兴安盟| 沧州市| 孝义市| 朝阳区| 连平县| 香河县| 达日县| 九龙县| 商水县| 疏附县| 长沙县| 阿鲁科尔沁旗| 栖霞市| 建瓯市| 岐山县| 崇阳县| 天水市| 东台市| 绥芬河市| 永川市| 沂南县| 辉县市| 敦化市| 门源| 成安县| 临朐县| 万安县| 芜湖县|