[已于 2023 年 10 月 5 日编辑以澄清查询]
我有一个
drawer menu
- 从左侧滑动屏幕,就会出现一个菜单标题列表,每个菜单标题下面都有 3-5 个子菜单项。向左滑动 activity_welcome.xml
打开抽屉菜单。该菜单由不可点击的标题组成,每个标题都有一个可点击的子菜单项。我无法让主要标题项目脱颖而出 - 例如不同的背景颜色、不同的字体大小和不同的字体颜色。举个简单的例子,以组标题“管理歌曲”和子菜单项为添加新歌曲、编辑歌曲、打开歌曲、搜索歌曲
我希望使组标题“管理歌曲”脱颖而出,但无法找到当前发生此情况的文件/代码。这是
DrawerLayout
:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
tools:openDrawer="start">
...
<!-- Navigation Drawer -->
<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"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu"
/>
</androidx.drawerlayout.widget.DrawerLayout>
这是我的
drawer_menu.xml
:
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Header: Manage Songs -->
<item
android:id="@+id/menu_item_manage_songs_header"
android:title="Manage Songs"
android:enabled="false"
app:showAsAction="always"
app:actionLayout="@menu/drawer_group_header"
android:background="@color/light_orange"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="@android:color/white" >
<menu>
<group android:id="@+id/group_songs">
<item
android:id="@+id/menu_item_add_new_song"
android:title="Add New Song"
android:onClick="onOptionsItemSelected" />
<item
android:id="@+id/menu_item_edit_songs"
android:title="Edit Songs"
android:onClick="onOptionsItemSelected" />
<item
android:id="@+id/menu_item_open_song"
android:title="Open Song"
android:onClick="onOptionsItemSelected" />
<item
android:id="@+id/menu_item_search_songs"
android:title="Search Songs"
android:onClick="onOptionsItemSelected" />
</group>
</menu>
</item>
...
</menu>
为了完整起见,我还将包括
themes.xml
:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.SongTracker" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/splash_background</item>
</style>
<style name="BackgroundLightGray" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/light_gray</item>
</style>
<style name="OrangeTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/orange</item>
</style>
</resources>
...和
AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.SongTracker"
tools:targetApi="31">
<activity
android:name=".addnewsong.AddNewSongActivity"
android:exported="false"
android:theme="@style/OrangeTheme"/>
<activity
android:name=".SplashActivity"
android:exported="true"
android:theme="@style/BackgroundLightGray">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".WelcomeActivity"
android:exported="false"
android:label="Welcome"
android:theme="@style/OrangeTheme" />
<activity
android:name=".AddUserActivity"
android:exported="false"
android:label="Add User"
android:screenOrientation="fullSensor"
android:theme="@style/OrangeTheme" />
<activity
android:name=".MainActivity"
android:exported="false">
</activity>
</application>
</manifest>
提前谢谢您 - 这看起来很简单。
这个问题不是很好理解,但要对每个菜单项执行操作,您需要实现一个侦听器。
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch(id){
case R.id.search:
//action
break;
}
return true;
}
}