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

安卓側滑菜單怎麼設置

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

閱讀全文

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

熱點內容
android仿ios時間選擇器 瀏覽:378
見識pdf 瀏覽:81
男孩子慰菊手冊pdf 瀏覽:532
注冊表啟動項命令 瀏覽:109
89c51單片機定時器 瀏覽:687
一般不適合做程序員的適合做啥 瀏覽:923
點在多邊形內演算法 瀏覽:494
程序員下班急忙回家 瀏覽:359
安慶php全套源碼交友類型網站源碼 瀏覽:92
浪潮伺服器公司地址 瀏覽:733
密約聊天交友app怎麼賺錢 瀏覽:280
滴滴java 瀏覽:109
phpexpires 瀏覽:218
matlab編程用什麼語言 瀏覽:70
php查找字元是否存在 瀏覽:701
可編程函數發生器 瀏覽:881
單片機正極接地視頻 瀏覽:67
利用python爬火車票 瀏覽:375
androidaidl如何工作 瀏覽:855
第三胸椎壓縮骨折什麼症狀 瀏覽:506