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

安卓側滑菜單怎麼設置

發布時間: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 表示菜單項的文字描述。

閱讀全文

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

熱點內容
查找ip地址命令 瀏覽:878
中國注冊會計師協會演算法人嗎 瀏覽:469
什麼叫伺服器內部錯誤4o4 瀏覽:494
單片機原理復習 瀏覽:435
打不開頭條任務伺服器怎麼辦 瀏覽:645
戰雷錄像在哪個文件夾 瀏覽:464
加密驗證的門禁卡 瀏覽:741
java輸出心 瀏覽:532
程序員的身體素質調查 瀏覽:383
python做一個登錄系統怎麼改界面 瀏覽:884
楊穎緩解壓力的方法 瀏覽:144
約苗app在哪裡改地址 瀏覽:819
tuxedo的加密協議 瀏覽:728
javapdftojpg 瀏覽:713
程序員軟考課程 瀏覽:274
Android框架工程師 瀏覽:387
計算機編程機器人工程師招聘 瀏覽:683
怎麼添加mite伺服器 瀏覽:581
方舟怎麼命令一隻恐龍手機版 瀏覽:712
javaqq郵箱發送 瀏覽:172