安卓:偏好设置样式

5
我已经创建了一个自定义偏好设置(即具有自定义布局的偏好设置),它显示在PreferenceActivity的偏好设置列表中。
布局是通过代码创建的。问题是在代码中创建的TextView字体看起来与Android的标准偏好设置字体有所不同。
因此,解决方案是将Android的偏好设置样式属性应用于我的TextView。相应的样式应该是preferenceScreenStylepreferenceStyle(我不确定)。
我的问题是我无法弄清楚如何读取Android的标准样式属性,以便我可以在代码中设置它们。
3个回答

6

我有同样的问题,但对于一些移动设备,如HTC Saphire和三星Galaxy S,我已经解决了它,但是我的HTC Desire HD出现了问题。您可以在android_SDK_resurces / layout / preference.xml中看到标准首选项样式。那里有边距,文字大小等。


4
抱歉打扰,但我在任何有关偏好样式的SO问题上都找不到这个答案。 我终于找到了答案:默认喜好现在使用layout/preference_material。 您可以在android源代码这里查看它和其他更具体的布局。为了防止链接失效,在下面复制了一份。
<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/listPreferredItemHeightSmall"
    android:gravity="center_vertical"
    android:paddingStart="?attr/listPreferredItemPaddingStart"
    android:paddingEnd="?attr/listPreferredItemPaddingEnd"
    android:background="?attr/activatedBackgroundIndicator"
    android:clipToPadding="false">
    <LinearLayout
        android:id="@+id/icon_frame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="-4dp"
        android:minWidth="60dp"
        android:gravity="start|center_vertical"
        android:orientation="horizontal"
        android:paddingEnd="12dp"
        android:paddingTop="4dp"
        android:paddingBottom="4dp">
        <com.android.internal.widget.PreferenceImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxWidth="48dp"
            android:maxHeight="48dp" />
    </LinearLayout>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingTop="16dp"
        android:paddingBottom="16dp">
        <TextView android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?attr/textAppearanceListItem"
            android:ellipsize="marquee" />
        <TextView android:id="@+id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            android:layout_alignStart="@id/title"
            android:textAppearance="?attr/textAppearanceListItemSecondary"
            android:textColor="?attr/textColorSecondary"
            android:maxLines="10"
            android:ellipsize="end" />
    </RelativeLayout>
    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout android:id="@+id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="end|center_vertical"
        android:paddingStart="16dp"
        android:orientation="vertical" />
</LinearLayout>

1
我已经成功解决了这个问题,方法是将当前自定义首选项的布局资源替换为标准首选项使用的布局资源,例如 EditTextPreference。以下是一个代码示例,注意 TimePreference 是自定义首选项。
    TimePreference wake_time = (TimePreference)findPreference("wake_time");
    EditTextPreference exercise = (EditTextPreference)findPreference("exercise");
    int r = exercise.getLayoutResource();
    wake_time.setLayoutResource(r);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接