在导航视图中,自定义主题创建工具栏时出现问题?

4

这是我第一次学习如何实现自定义Android主题,我不太了解我的styles.xml代码。我希望能根据用户选择的主题在运行时设置主题。如果我不包括Toolbar,我不知道如何设置Toggle并强制NavigationView覆盖ActionBar。应用程序无法打开,并显示以下错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kotlinappv3/com.example.kotlinappv3.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2928)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3063)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1823)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:198)
    at android.app.ActivityThread.main(ActivityThread.java:6729)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
    at androidx.appcompat.app.AppCompatDelegateImpl.setSupportActionBar(AppCompatDelegateImpl.java:345)
    at androidx.appcompat.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)
    at com.example.kotlinappv3.MainActivity.onCreate(MainActivity.kt:35)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3063) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1823) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:198) 
    at android.app.ActivityThread.main(ActivityThread.java:6729) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858

这是我的styles.xml文件:
<style name="MyLightTheme" parent="Theme.AppCompat.Light"/>

<style name="NoActionBar" parent="MyLightTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBarOverlay">true</item>
</style>

<style name="MyLightTheme.Red">
    <item name="colorPrimary">@color/red_4_100</item>
    <item name="colorPrimaryDark">@color/red_6_100</item>
    <item name="colorAccent">@color/red_6_100</item>
</style>

<!-- Some more colors ...
<style name="MyLightTheme.ColorName"> -->

这是 AndroidManifest.xml 文件:

<activity
    android:name=".MainActivity"
    android:theme="@style/NoActionBar">

这是activity_main.xml文件:

<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start" >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <include layout="@layout/toolbar"/>
        <FrameLayout
            android:id="@+id/fragment_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    <include layout="@layout/navigation_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>

下面是来自MainActivity.kt类的代码:

override fun onCreate(savedInstanceState: Bundle?) {
    setTheme(MyThemes(this).getMyThemeId())    // setTheme(R.style.MyLightTheme_Red)
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    if (savedInstanceState == null) { supportFragmentManager.beginTransaction().replace(R.id.fragment_main, MyFragment()).commit() }

    val toolbar: Toolbar = findViewById(R.id.toolbar)
    setSupportActionBar(toolbar)

    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
    val toggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
    drawerLayout.addDrawerListener(toggle)
    toggle.syncState()

    val navView: NavigationView = findViewById(R.id.nav_view)
    navView.setNavigationItemSelectedListener(this)
}
3个回答

2

setTheme(MyThemes(this).getMyThemeId()) // setTheme(R.style.MyLightTheme_Red)

您自己说了,您正在将主题设置为MyLightTheme_Red

最初的回答。

<style name="MyLightTheme.Red">
    <item name="colorPrimary">@color/red_4_100</item>
    <item name="colorPrimaryDark">@color/red_6_100</item>
    <item name="colorAccent">@color/red_6_100</item>
</style>

那个代码并没有排除工具栏,因为它没有父级元素。只需要添加:
<style name="MyLightTheme.Red" parent="NoActionBar">

现在它将继承该样式的“无工具栏”属性。最初的回答。

谢谢答复,我也在github上找到了一个例子。 - Akshdeep Singh

1

如果要使用 AndroidX,请在您的 res/values/styles.xml 中使用此主题:

<style name="MyLightTheme.NoActionBar" parent="Theme.MaterialComponents.Light.NoActionBar">
...
</style>

将此主题添加到您的 AndroidManifest.xml 文件中:
<activity
    android:name=".MainActivity"
    android:theme="@style/MyLightTheme.NoActionBar">

0

使用以下代码更改您的样式为NoActionBar

<style name="MyLightTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
...
</style>

而且,不需要添加

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

并且在 Manifest.xml

<activity
    android:name=".MainActivity"
    android:theme="@style/MyLightTheme.NoActionBar">

这样,我可以在操作栏下方获得工具栏,但我不知道如何在操作栏上设置切换。 - Akshdeep Singh
你确定在活动清单中更改了主题吗? - Gilbert Arafat
我没有在活动清单中更改主题,而是在主活动的oncreate方法中更改它。 - Akshdeep Singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接