简体   繁体   中英

Problems with create custom PreferenceScreen and mange each one in SettingsFragment - kotlin

According to this question I am trying to create a custom layout and add it to my root PreferenceScreen XML, but I am facing some problems, first, the switch_preference_layout.xml looks corrupted when I add it inside the Preference tag

here's my custom layout for the switch to dark mode example

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@android:id/widget_frame"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="@dimen/_8sdp"
    android:weightSum="2">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:orientation="vertical">

        <TextView
            android:id="@android:id/title"
            style="@style/TextAppearance.PreferenceTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:text="Dark Mode"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@android:id/summary"
            style="@style/TextAppearance.PreferenceSummary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:text="Improve visibility and save energy" />

    </LinearLayout>

    <androidx.appcompat.widget.SwitchCompat
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_weight="1" />
</LinearLayout>

and it looks like the following

在此处输入图像描述

after adding it in root_prefernces.xml using android:widgetLayout="@layout/switch_preference_layout" it looks like that

在此处输入图像描述

it's almost invisible, , I tried also to use android:layout="@layout/switch_preference_layout" and it completely disappeared

在此处输入图像描述

the full code of root_prefernces.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen 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">

    <PreferenceCategory>
        <Preference
            android:defaultValue="false"
            android:key="switchToDarkMode"
            android:widgetLayout="@layout/switch_preference_layout">
        </Preference>

    </PreferenceCategory>

    <Preference
        android:title="Publisher info"
        app:key="aboutPublisher">

    </Preference>

    <Preference
        android:title="@string/about"
        app:key="@string/about">


    </Preference>

</PreferenceScreen>

Second Problem

When I try to extend `Preference` in `SettingsFragment` I see working and it's required to pass `context` in its constructor unlike the other `Preference` class in this [answer][5]

在此处输入图像描述

and it also needs to override some methods and the last one isVisible the android studio shows working message 'isVisible' in 'PreferenceFragmentCompat' is final and cannot be overridden and if I removed it's showing it's required to override it, I am not sure but it looks like a bug

在此处输入图像描述

First problem:

The widgetLayout is the layout for the widget on the right only, not the title and subject. So in your case, your entire layout should contain solely a SwitchCompat.

Second problem:

The answer in the question you linked does include a Context parameter in its constructor. Note that it's written in Java, and its constructor signature is:

public ImageViewPreference(Context context, AttributeSet attrs)

There should be no reason to override isVisible since that is just for querying whether the preference is set to be visible in the hierarchy. If you want to customize when the preference should be visible, you can call visible = in the constructor.

From looking at your code so far, it seems to me like you should subclass SwitchPreferenceCompat instead of Preference so you won't have to deal with a layout at all or with programming how to persist the value. If the only logic you want to add is setting the visibility of the preference under certain conditions, personally, I wouldn't subclass it at all. Instead, I would just set its visibility at runtime. Depending on what the condition is, you might be able to set it in the XML preferences file directly by using a Boolean resource.


I'm suggesting to use the already existing SwitchPreferenceCompat:

<SwitchPreferenceCompat
    app:title="@string/darkModePreferenceTitle"
    app:summary="@string/darkModePreferenceSummary"
    app:defaultValue="false"
    app:key="switchToDarkMode" />

Where darkModePreferenceTitle and darkModePreferenceSummary are String resources for "Dark mode" and "Improve visibility and save energy".

<!-- In strings.xml -->
<string name="darkModePreferenceTitle">Dark mode</string>
<string name="darkModePreferenceSummary">Improve visibility and save energy</string>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM