抬头仰望星空,是否能发现自己的渺小。

伪斜杠青年

人们总是混淆了欲望和理想

SwitchPreferenceCompat自定义适配

设置项中的控件定义不知道的时候挺麻烦的,知道后就好很多了,所以记录下。

对于这种控件,一般采用theme的形式进行一次性替换,然后在对应的activity中去应用,定义一个style继承安卓SDK中的style,分Dark和Light两种,这里以Light为例:

<style name="ThemeOverlay.Preferences" parent="Theme.AppCompat.Light">
    <item name="switchPreferenceCompatStyle">@style/Preference.SwitchPreferenceCompat
    </item>
</style>

对应的描述字段:switchPreferenceCompatStyle,同时再新增字段继承Material的style

<style name="Preference.SwitchPreferenceCompat" parent="Preference.SwitchPreferenceCompat.Material">
    <item name="android:layout">@layout/preference_material</item>
    <item name="android:widgetLayout">@layout/preference_widget_switch_compat</item>
</style>

preference_material.xml布局:

<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2014 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<!-- Layout for a Preference in a PreferenceActivity. The
     Preference is able to place a specific widget for its particular
     type in the "widget_frame" layout. -->

<LinearLayout 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="wrap_content"
    android:clipToPadding="false"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:orientation="horizontal"
    android:paddingStart="0dp"
    android:paddingEnd="21dp">

    <FrameLayout
        android:id="@+id/icon_frame"
        android:layout_width="64dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <com.android.internal.widget.PreferenceImageView
            android:id="@android:id/icon"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_gravity="center" />
    </FrameLayout>

    <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="?textAppearanceListItem" />

        <TextView
            android:id="@android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignStart="@android:id/title"
            android:layout_marginTop="2dp"
            android:ellipsize="end"
            android:lineSpacingExtra="5dp"
            android:maxLines="10"
            android:textAppearance="?textAppearanceListItemSecondary"
            android:textColor="?android:attr/textColorSecondary" />

    </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:paddingEnd="0dp" />

</LinearLayout>

preference_widget_switch_compat.xml如下:

<SwitchCompat 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"
    android:id="@+id/switchWidget"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@null"
    android:clickable="false"
    android:focusable="false"
    android:switchMinWidth="38dp"
    android:thumb="@drawable/switch_thumb_selector"
    android:track="@drawable/switch_track_selector"
    tools:checked="true" />

这里想说的是android:switchMinWidth只有在设置了android:thumbandroid:track后才有效。其他没什么特别的。

然后在相关activity用上theme就可以了,就是要自定义的状态很多,亮色暗色禁用什么的。


本站由以下主机服务商提供服务支持:

0条评论

发表评论