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

安卓側滑菜單怎麼設置

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

閱讀全文

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

熱點內容
小天才app的應用中心有什麼應用 瀏覽:768
java代碼整理 瀏覽:259
安卓單個胎壓怎麼配對 瀏覽:627
linux將文件清空 瀏覽:476
一套前端編譯平台 瀏覽:598
安卓9x用什麼框架 瀏覽:72
萬用表怎樣量壓縮機漏電 瀏覽:548
無線路由器雲登錄伺服器未連接 瀏覽:781
aes是公鑰密碼演算法 瀏覽:698
linuxphp編譯參數 瀏覽:534
安卓手機怎麼永久關閉後台啟動 瀏覽:40
網站phpjavascript 瀏覽:453
64位java內存 瀏覽:418
女程序員學習方法 瀏覽:383
工程數學線性代數pdf 瀏覽:681
提升程序員技術檔次的書 瀏覽:691
python詞雲圖txt格式 瀏覽:968
韓國料理pdf 瀏覽:227
什麼app就能知道自己的臉型 瀏覽:383
准了app月卡可以看什麼 瀏覽:140