【问题标题】:Android, how to add toolbar in my Preferences?Android,如何在我的首选项中添加工具栏?
【发布时间】:2019-10-19 14:59:43
【问题描述】:

首先,我使用的是 Jetpack 导航组件 - 导航抽屉。

而“无操作栏”是我应用的主要风格。

    <style name="NoActionBar" parent="AppTheme">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="preferenceTheme">@style/PrefsTheme</item>
    </style>

这是清单。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xxx.xxx">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:name=".MainApplication"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".ui.MainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

因为我使用的是 Jetpack Navigation Component,所以它是一个单一的活动模式。

一般来说,我的 UI 有一个自定义工具栏。 没关系。

但在设置 UI 的情况下,我遇到了问题...

我正在使用 Jetpack 首选项。 https://developer.android.com/guide/topics/ui/settigs

我的设置 UI 扩展了“PreferenceFragmentCompat”,它也在“nav_graph.xml”中定义。

所以可以通过以下代码访问:

    findNavController().navigate(R.id.settingsFragment)

因为我的应用使用“NoActionBar”,所以首选项也没有操作栏。 并且偏好是由 Jetpack Preferences 做出的,我无法添加自定义工具栏。

请问有什么好的想法/解决方案吗?

【问题讨论】:

    标签: android android-preferences android-jetpack android-jetpack-navigation


    【解决方案1】:

    可以有多种解决方案。创建另一种样式,使操作栏为真。

    <style name="AppTheme.WithActionBar" parent="AppTheme">
        <item name="windowActionBar">true</item>
        <item name="windowNoTitle">false</item>
        <item name="preferenceTheme">@style/PrefsTheme</item>
    </style>
    

    然后仅在 Manifest 内将主题专门应用于您想要的活动。

    <activity
            android:name="YourActivity"
            android:theme="@style/AppTheme.WithActionBar" />
    

    【讨论】:

    • 我的偏好在同一个活动中......根据您的意见,我应该将我的偏好移到另一个导航图上。但它看起来不像我想要的解决方案。
    • @yoonhok 请添加您的一些活动代码以及您如何在清单中定义活动以更好地理解问题
    • 我添加了完整的清单代码。它只有一个活动,因为我使用 Jetpack 导航组件。
    【解决方案2】:

    我正在使用喷气背包导航抽屉MainActivity 没有Actionbar,我也喜欢Actionbar,只是我正在使用AppTheme

    <activity
            android:name="YourPreferenceActivity"
            android:theme="@style/AppTheme" />
    

    注意:如果您的默认设置,您可以删除PreferenceActivity 中的主题 主题是AppTheme

    没有操作栏主题的 MainActivity

    <activity
                android:name="com.jca.dastuurkajsl.ui.main.MainActivity"
                android:label="@string/app_name"
                android:theme="@style/AppTheme.NoActionBar">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    

    这是我的 AppTheme 的样子

    <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
    </style>
    

    AppTheme.NoActionBar

    <style name="AppTheme.NoActionBar">
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</item>
        </style>
    

    【讨论】:

    • 是的,我正在使用带有导航抽屉的 Jetpack 导航组件。但我的偏好是在 nav_graph 中。
    • 所以我认为你没有使用Activity来保存偏好的片段
    猜你喜欢
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 2014-10-13
    • 2020-10-12
    相关资源
    最近更新 更多