導航:首頁 > 操作系統 > 安卓側滑菜單怎麼設置

安卓側滑菜單怎麼設置

發布時間:2025-01-04 09:48:30

『壹』 androidQQ側滑菜單代碼

下面是一個簡單的 Android QQ 側滑菜單代碼實現示例:

public class MainActivity extends AppCompatActivity {

private DrawerLayout drawerLayout;

private ActionBarDrawerToggle toggle;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

drawerLayout = findViewById(R.id.drawer_layout);

toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);

drawerLayout.addDrawerListener(toggle);

toggle.syncState();

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

NavigationView navigationView = findViewById(R.id.nav_view);

navigationView.(new NavigationView.() {

@Override

public boolean onNavigationItemSelected(@NonNull MenuItem item) {

// 處理菜單項點擊事件

drawerLayout.closeDrawer(GravityCompat.START);

return true;

}

});

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

if (toggle.onOptionsItemSelected(item)) {

return true;

}

return super.onOptionsItemSelected(item);

}

}

<?xml version="1.0" encoding="utf-8"?>

<androidx.drawerlayout.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">

<!-- 主布局 -->

<RelativeLayout

android:layout_width="match_parent"

android:layout_height="match_parent">

<!-- 添加主界面的控制項 -->

</RelativeLayout>

<!-- 側滑菜單布局 -->

<com.google.android.material.navigation.NavigationView

android:id="@+id/nav_view"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:layout_gravity="start"

app:headerLayout="@layout/nav_header"

app:menu="@menu/nav_menu" />

</androidx.drawerlayout.widget.DrawerLayout>

其中,@layout/nav_header 表示側滑菜單的頭部布局,@menu/nav_menu 表示側滑菜單的菜單項布局。

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<group android:checkableBehavior="single">

<item

android:id="@+id/nav_home"

android:icon="@drawable/ic_home"

android:title="@string/menu_home" />

<item

android:id="@+id/nav_gallery"

android:icon="@drawable/ic_gallery"

android:title="@string/menu_gallery" />

<item

android:id="@+id/nav_slideshow"

android:icon="@drawable/ic_slideshow"

android:title="@string/menu_slideshow" />

</group>

</menu>

其中,@drawable/ic_XXX 表示菜單項的圖標,@string/menu_XXX 表示菜單項的文字描述。

閱讀全文

與安卓側滑菜單怎麼設置相關的資料

熱點內容
資料庫查詢系統源碼 瀏覽:617
php5314 瀏覽:358
完美國際安裝到哪個文件夾 瀏覽:669
什麼app可以掃一掃做題 瀏覽:540
程序員編碼論壇 瀏覽:924
淘點是什麼app 瀏覽:660
中國高等植物pdf 瀏覽:454
51單片機時間 瀏覽:182
後台如何獲取伺服器ip 瀏覽:267
單片機流水燈程序c語言 瀏覽:236
程序員第二職業掙錢 瀏覽:240
運行里怎麼輸入伺服器路徑 瀏覽:843
pythonstepwise 瀏覽:512
劉一男詞彙速記指南pdf 瀏覽:66
php認證級別 瀏覽:371
方舟編譯啥時候推送 瀏覽:1012
php手機驗證碼生成 瀏覽:677
哲學思維pdf 瀏覽:17
凌達壓縮機有限公司招聘 瀏覽:535
weblogic命令部署 瀏覽:39