工具栏在导航视图中为自定义主题创建问题?

时间:2019-06-29 10:20:28

标签: android kotlin android-toolbar android-theme

这是我第一次学习实现自定义Android主题,我不太了解styles.xml代码。我想根据用户对主题的选择在运行时设置主题。如果不包括Toolbar,则不知道如何设置Toggle并将NavigationView强制置于ActionBar之上。该应用无法打开,并因以下error崩溃:

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 个答案:

答案 0 :(得分: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">

现在,它将继承该样式的“无工具栏”属性。

答案 1 :(得分: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">

答案 2 :(得分: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">