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

安卓側滑菜單怎麼設置

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

閱讀全文

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

熱點內容
單片機的Vpp是 瀏覽:350
iua編譯器下載官方 瀏覽:84
壓縮機高低壓快速平衡 瀏覽:873
phpai 瀏覽:707
怎麼不被命令 瀏覽:86
大話緣定三生伺服器什麼便宜 瀏覽:967
idea編譯內部類 瀏覽:466
pdf2word在線轉換 瀏覽:588
tim儲存在哪個文件夾 瀏覽:621
華碩電腦u盤加密最簡單方法 瀏覽:853
編程過路馬游戲 瀏覽:608
python多重線性回歸 瀏覽:516
冰箱壓縮機工作圖 瀏覽:843
OTC機器人編程師 瀏覽:948
簽名的apk能防止反編譯嗎 瀏覽:311
我的世界伺服器如何版本 瀏覽:285
集合競價買入額指標源碼 瀏覽:114
雲伺服器安裝華為歐拉系統 瀏覽:959
c面向對象編程教程 瀏覽:527
php的重定向跳轉頁面跳轉 瀏覽:411