簡體   English   中英

androidx 中的自定義 SwitchPreferenceCompat

[英]Custom SwitchPreferenceCompat in androidx

我正在嘗試將首選項屏幕中的默認開關拇指更改為我的自定義。 我嘗試了在這里找到的不同解決方案,但沒有一個有效。 我最后一次嘗試是創建自定義布局,但問題是我無法正確地將開關添加到該布局。 由於 id 無法真正添加 androidx.appcompat.widget.SwitchCompat(@android:id/switch_widget 需要 API 級別 24(當前最小值為 21)或其他建議的 id:無法解析符號 '@android:id/switchWidget' )。 root_preferences.xml(部分):

<PreferenceCategory app:title="@string/confidentiality"
    android:layout="@layout/preferences_category">

    <SwitchPreferenceCompat
        android:layout="@layout/switch_preference_compat"
        app:key="show_contacts"
        app:title="@string/contact_information"
        app:singleLineTitle="false"
        app:defaultValue="true"
        app:iconSpaceReserved="false"/>
</PreferenceCategory>

switch_preference_compat.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:orientation="vertical">

        <TextView
            android:id="@android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/ks_font_data"
            android:textSize="18sp"/>

        <TextView
            android:id="@android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"/>

    </LinearLayout>

    <androidx.appcompat.widget.SwitchCompat
        android:id="@android:id/switchWidget" <--- error here
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

我試過 styles 但它也不能正常工作。 我希望我的拇指看起來像這樣[我的自定義開關][1] [1]:https://i.stack.imgur.com/Js5vb.png

因此,幾個小時后,我能夠找到如何做到這一點。 發布此答案,以便它可以幫助某人節省時間。 在 root_preferences.xml 中有android:widgetLayout屬性,您可以在其中為 SwitchPreferenceCompat 設置自定義布局:

    <SwitchPreferenceCompat
        android:widgetLayout="@layout/switch_preference_compat"
        app:key="show_contacts"
        app:title="@string/contact_information"
        app:singleLineTitle="false"
        app:defaultValue="true"
        app:iconSpaceReserved="false"/>

switch_preference_compat.xml:

<?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:orientation="horizontal"
    android:gravity="center_vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:orientation="vertical">

        <TextView
            android:id="@android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"/>

        <TextView
            android:id="@android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"/>

    </LinearLayout>

    <androidx.appcompat.widget.SwitchCompat
        android:id="@+id/switchWidget"
        android:thumb="@drawable/thumb"
        app:track="@drawable/track"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout> 

您可以在其中放置用於自定義拇指和軌道的可繪制對象。 我的看起來是這樣的:thumb.xml:

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

    <item android:state_checked="false"
        android:drawable="@drawable/switch_icon_false" />

    <item android:state_checked="true"
        android:drawable="@drawable/switch_icon_true"/>

</selector>

軌道.xml:

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

    <item android:state_checked="false">
        <shape android:shape="rectangle">
            <solid android:color="#dedede"/>
            <corners android:radius="100sp"/>
            <stroke android:color="#dedede"
                android:width="1dp"/>
        </shape>
    </item>

    <item android:state_checked="true">
        <shape android:shape="rectangle">
            <solid android:color="#9dcbb5"/>
            <corners android:radius="100sp"/>
        </shape>
    </item>

</selector>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM