(一百九十八)Android Q 学习Preference组件

目录

1.Preference定义

2.默认布局

3.默认style


 

 

1.Preference定义

/frameworks/base/core/java/android/preference/Preference.java

 * @deprecated Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
 *      <a href="{@docRoot}reference/androidx/preference/package-summary.html">
 *      Preference Library</a> for consistent behavior across all devices. For more information on
 *      using the AndroidX Preference Library see
 *      <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>.
 */
@Deprecated
public class Preference implements Comparable<Preference> {
...
    /**
     * Perform inflation from XML and apply a class-specific base style. This
     * constructor of Preference allows subclasses to use their own base style
     * when they are inflating. For example, a {@link CheckBoxPreference}
     * constructor calls this version of the super class constructor and
     * supplies {@code android.R.attr.checkBoxPreferenceStyle} for
     * <var>defStyleAttr</var>. This allows the theme's checkbox preference
     * style to modify all of the base preference attributes as well as the
     * {@link CheckBoxPreference} class's attributes.
     *
     * @param context The Context this is associated with, through which it can
     *            access the current theme, resources,
     *            {@link SharedPreferences}, etc.
     * @param attrs The attributes of the XML tag that is inflating the
     *            preference.
     * @param defStyleAttr An attribute in the current theme that contains a
     *            reference to a style resource that supplies default values for
     *            the view. Can be 0 to not look for defaults.
     * @param defStyleRes A resource identifier of a style resource that
     *            supplies default values for the view, used only if
     *            defStyleAttr is 0 or can not be found in the theme. Can be 0
     *            to not look for defaults.
     * @see #Preference(Context, AttributeSet)
     */
    public Preference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        mContext = context;

        final TypedArray a = context.obtainStyledAttributes(
                attrs, com.android.internal.R.styleable.Preference, defStyleAttr, defStyleRes);
        for (int i = a.getIndexCount() - 1; i >= 0; i--) {
            int attr = a.getIndex(i);
            switch (attr) {
                case com.android.internal.R.styleable.Preference_icon:
                    mIconResId = a.getResourceId(attr, 0);
                    break;

                case com.android.internal.R.styleable.Preference_key:
                    mKey = a.getString(attr);
                    break;

                case com.android.internal.R.styleable.Preference_title:
                    mTitleRes = a.getResourceId(attr, 0);
                    mTitle = a.getText(attr);
                    break;

                case com.android.internal.R.styleable.Preference_summary:
                    mSummary = a.getText(attr);
                    break;

                case com.android.internal.R.styleable.Preference_order:
                    mOrder = a.getInt(attr, mOrder);
                    break;

                case com.android.internal.R.styleable.Preference_fragment:
                    mFragment = a.getString(attr);
                    break;

                case com.android.internal.R.styleable.Preference_layout:
                    mLayoutResId = a.getResourceId(attr, mLayoutResId);
                    break;

                case com.android.internal.R.styleable.Preference_widgetLayout:
                    mWidgetLayoutResId = a.getResourceId(attr, mWidgetLayoutResId);
                    break;

                case com.android.internal.R.styleable.Preference_enabled:
                    mEnabled = a.getBoolean(attr, true);
                    break;

                case com.android.internal.R.styleable.Preference_selectable:
                    mSelectable = a.getBoolean(attr, true);
                    break;

                case com.android.internal.R.styleable.Preference_persistent:
                    mPersistent = a.getBoolean(attr, mPersistent);
                    break;

                case com.android.internal.R.styleable.Preference_dependency:
                    mDependencyKey = a.getString(attr);
                    break;

                case com.android.internal.R.styleable.Preference_defaultValue:
                    mDefaultValue = onGetDefaultValue(a, attr);
                    break;

                case com.android.internal.R.styleable.Preference_shouldDisableView:
                    mShouldDisableView = a.getBoolean(attr, mShouldDisableView);
                    break;

                case com.android.internal.R.styleable.Preference_recycleEnabled:
                    mRecycleEnabled = a.getBoolean(attr, mRecycleEnabled);
                    break;

                case com.android.internal.R.styleable.Preference_singleLineTitle:
                    mSingleLineTitle = a.getBoolean(attr, mSingleLineTitle);
                    mHasSingleLineTitleAttr = true;
                    break;

                case com.android.internal.R.styleable.Preference_iconSpaceReserved:
                    mIconSpaceReserved = a.getBoolean(attr, mIconSpaceReserved);
                    break;
           }
        }
        a.recycle();
    }

    /**
     * Perform inflation from XML and apply a class-specific base style. This
     * constructor of Preference allows subclasses to use their own base style
     * when they are inflating. For example, a {@link CheckBoxPreference}
     * constructor calls this version of the super class constructor and
     * supplies {@code android.R.attr.checkBoxPreferenceStyle} for
     * <var>defStyleAttr</var>. This allows the theme's checkbox preference
     * style to modify all of the base preference attributes as well as the
     * {@link CheckBoxPreference} class's attributes.
     *
     * @param context The Context this is associated with, through which it can
     *            access the current theme, resources,
     *            {@link SharedPreferences}, etc.
     * @param attrs The attributes of the XML tag that is inflating the
     *            preference.
     * @param defStyleAttr An attribute in the current theme that contains a
     *            reference to a style resource that supplies default values for
     *            the view. Can be 0 to not look for defaults.
     * @see #Preference(Context, AttributeSet)
     */
    public Preference(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    /**
     * Constructor that is called when inflating a Preference from XML. This is
     * called when a Preference is being constructed from an XML file, supplying
     * attributes that were specified in the XML file. This version uses a
     * default style of 0, so the only attribute values applied are those in the
     * Context's Theme and the given AttributeSet.
     *
     * @param context The Context this is associated with, through which it can
     *            access the current theme, resources, {@link SharedPreferences},
     *            etc.
     * @param attrs The attributes of the XML tag that is inflating the
     *            preference.
     * @see #Preference(Context, AttributeSet, int)
     */
    public Preference(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.preferenceStyle);
    }

    /**
     * Constructor to create a Preference.
     *
     * @param context The Context in which to store Preference values.
     */
    public Preference(Context context) {
        this(context, null);
    }

 

2.默认布局

    @UnsupportedAppUsage
    private int mLayoutResId = com.android.internal.R.layout.preference;

可以看到布局其实很简单

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 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" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingEnd="?android:attr/scrollbarSize"
    android:background="?android:attr/selectableItemBackground" >

    <ImageView
        android:id="@+android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dip"
        android:layout_marginEnd="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <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:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4" />

    </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="center_vertical"
        android:orientation="vertical" />

</LinearLayout>

 

3.默认style

    public Preference(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.preferenceStyle);
    }
jiatai@jiatai:~/expand/aosp/Q_source_android-10.0.0_r6/frameworks/base/core/res/res$ grep "PreferenceStyle" ./ -nriw
./values/attrs.xml:920:        <attr name="preferenceStyle" format="reference" />

看下主体里怎么定义的

./values/themes.xml:320:        <item name="preferenceStyle">@style/Preference</item>

去style里看一下

jiatai@jiatai:~/expand/aosp/Q_source_android-10.0.0_r6/frameworks/base/core/res/res$ grep "\"Preference\"" ./ -nriw --include="styles.xml" -C3
./values/styles.xml-990-
./values/styles.xml-991-    <!-- Preference Styles -->
./values/styles.xml-992-
./values/styles.xml:993:    <style name="Preference">
./values/styles.xml-994-        <item name="layout">@layout/preference</item>
./values/styles.xml-995-    </style>
./values/styles.xml-996-

只定义了它的布局

 

4.属性声明

Q_source_android-10.0.0_r6/frameworks/base/core/res/res$ gedit +7523 ./values/attrs.xml

    <!-- Base attributes available to Preference. -->
    <declare-styleable name="Preference">
        <!-- The optional icon for the preference. -->
        <attr name="icon" />
        <!-- The key to store the Preference value. -->
        <attr name="key" format="string" />
        <!-- The title for the Preference. In API 25 and earlier, this value is read as a
         plain string with styling information stripped. -->
        <attr name="title" />
        <!-- The summary for the Preference. In API 25 and earlier, this value is read as a
         plain string with styling information stripped. -->
        <attr name="summary" />
        <!-- The order for the Preference (lower values are to be ordered first). If this is not
             specified, the default ordering will be alphabetic. -->
        <attr name="order" format="integer" />
        <!-- When used inside of a modern PreferenceActivity, this declares
             a new PreferenceFragment to be shown when the user selects this item. -->
        <attr name="fragment" />
        <!-- The layout for the Preference in a PreferenceActivity screen. This should
             rarely need to be changed, look at widgetLayout instead. -->
        <attr name="layout" />
        <!-- The layout for the controllable widget portion of a Preference. This is inflated
             into the layout for a Preference and should be used more frequently than
             the layout attribute. For example, a checkbox preference would specify
             a custom layout (consisting of just the CheckBox) here. -->
        <attr name="widgetLayout" format="reference" />
        <!-- Whether the Preference is enabled. -->
        <attr name="enabled" />
        <!-- Whether the Preference is selectable. -->
        <attr name="selectable" format="boolean" />
        <!-- The key of another Preference that this Preference will depend on.  If the other
             Preference is not set or is off, this Preference will be disabled. -->
        <attr name="dependency" format="string" />
        <!-- Whether the Preference stores its value to the storage. -->
        <attr name="persistent" />
        <!-- The default value for the preference, which will be set either if persistence
             is off or persistence is on and the preference is not found in the persistent
             storage.  -->
        <attr name="defaultValue" format="string|boolean|integer|reference|float" />
        <!-- Whether the view of this Preference should be disabled when
             this Preference is disabled. -->
        <attr name="shouldDisableView" format="boolean" />
        <!-- Whether the preference has enabled to have its view recycled when used in the list
             view. This is true by default. -->
        <attr name="recycleEnabled" format="boolean" />
        <!-- Whether to use single line for the preference title text. By default, preference title
             will be constrained to one line, so the default value of this attribute is true. -->
        <attr name="singleLineTitle" format="boolean" />
        <!-- Whether the space for the preference icon view will be reserved. By default, preference
             icon view visibility will be set to GONE when there is no icon provided, so the default
             value of this attribute is false. -->
        <attr name="iconSpaceReserved" format="boolean" />
    </declare-styleable>

 

相关推荐
©️2020 CSDN 皮肤主题: 鲸 设计师:meimeiellie 返回首页