【问题标题】:Theming PreferenceFragmentCompat主题偏好FragmentCompat
【发布时间】:2021-02-22 19:37:29
【问题描述】:

我在将主题设置为 PreferenceFragmentCompat 以尝试实现此目的时遇到了一些麻烦:

我设置了以下样式:

<style name="AppThemeBase" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="android:windowLightStatusBar">true</item>
    <item name="android:statusBarColor">@color/status_bar</item>

    <item name="colorControlNormal">@color/colorPrimary</item>
    <item name="preferenceTheme">@style/AppThemeBase.RecordingPreferencesTheme</item>
</style>

<style name="AppThemeBase.RecordingPreferencesTheme" parent="@style/PreferenceThemeOverlay">
    <item name="android:textColorPrimary">#FD0000</item>
    <item name="android:textColorSecondary">#1F6F2B</item>
</style>

使用此选项,我可以在 Android Studio 布局预览中看到我想要实现的目标,但是当我执行它时它没有效果

我执行时的结果:

到目前为止,它唯一有效的方法是使用 &lt;item name="android:textColor"&gt;#FD0000&lt;/item&gt; 来更改标题颜色,但我找不到更改摘要颜色的方法。

我的另一个问题是,是否有一种方法可以仅为该首选项片段设置主题,而不是在所有首选项片段中使用它。在片段布局的根部使用android:theme 对我不起作用:(

更新:

根据@Susan 的问题,这是我尝试在片段布局的根部使用android:theme 的方式,我在主布局的基础上创建了这个主题

<style name="RecordingPreferenceTheme" parent="AppThemeBase">
    <item name="preferenceTheme">@style/AppThemeBase.RecordingPreferencesTheme</item>
</style>

这是首选项布局,其中我有一个片段会膨胀PreferenceFragmentCompat

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/RecordingPreferenceTheme">

<include
    android:id="@+id/include"
    layout="@layout/toolbar"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<fragment
    android:id="@+id/fragment_container_settings"
    android:name="com.example.app.RecordSettingsFragment$RecordPreferences"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/include" />

</androidx.constraintlayout.widget.ConstraintLayout>

并且在布局的根部我会设置android:theme="@style/RecordingPreferenceTheme",但它不起作用,只是在Activity中设置的主主题的变化会有一些效果。我已经尝试过customize PreferenceFragmentCompat layout,结果相同。

干杯!!

【问题讨论】:

  • 你能分享一下你是如何在片段的根目录中应用android:theme的吗?
  • @SusanThapa 我已经更新了问题
  • 不可能仅仅为了偏好而改变颜色,如果你想这样做,你必须从像Preference这样的特定类扩展,并且还必须复制它的布局xml 到您的应用程序以具有这些硬编码值

标签: android android-fragments android-theme preferencefragmentcompat


【解决方案1】:

查看此示例项目here 为您的特定情况创建此解决方案的唯一问题是您必须从代码中创建您的首选项,不能使用 xml 文件来膨胀和 对于那些只想要这个解决方案的要点的人来说,使用 setLayoutResource() 方法来设置布局并使用自定义布局我只是根据@Bhuntupana 的需要复制了原始布局和硬编码颜色

val messagesCategory = PreferenceCategory(context).apply {
                layoutResource = R.layout.sample_preference
                key = "messages_category"
                title = getString(R.string.messages_header)
                screen.addPreference(this)
            }

更新

使用布局标签来指定布局资源像这样使用

        <EditTextPreference
            android:layout="@layout/sample_preference"
            app:key="signature"
            app:title="@string/signature_title"
            app:useSimpleSummaryProvider="true" />

sample_preference.xml 只是默认布局的副本preference_material.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:baselineAligned="false"
    android:clipToPadding="false"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:paddingRight="?android:attr/listPreferredItemPaddingRight">

    <include layout="@layout/image_frame" />

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingTop="16dp"
        android:paddingBottom="16dp">

        <TextView
            android:id="@android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceListItem"
            android:textColor="#FD0000" />

        <TextView
            android:id="@android:id/summary"
            style="@style/PreferenceSummaryTextStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignStart="@android:id/title"
            android:layout_alignLeft="@android:id/title"
            android:layout_gravity="start"
            android:maxLines="10"
            android:textAlignment="viewStart"
            android:textColor="#1F6F2B" />

    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout
        android:id="@android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="end|center_vertical"
        android:orientation="vertical"
        android:paddingStart="16dp"
        android:paddingLeft="16dp"
        android:paddingEnd="0dp"
        android:paddingRight="0dp" />

</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多