我做了一个自定义偏好(即具有自定义布局的首选项),显示在首选项列表中 PreferenceActivity.

布局是用代码创建的。问题在于,在代码中创建的 TextView 的字体看起来与 Android 的标准偏好字体有些不同。

所以解决方案是将 android 偏好的样式属性应用到我的 TextView. 。各自的风格应该是 偏好屏幕样式 或者 偏好风格 (我不知道)。

我的问题是我不知道如何读取android的标准样式属性,所以我可以在代码中设置它们。

有帮助吗?

解决方案

我也有同样的问题,但我已针对某些移动设备(HTC Saphire 和 Samsung Galaxy S)修复了该问题,但我的 HTC Desire HD 遇到了问题。您可以在 android_SDK_resurces/layout/preference.xml 中看到标准首选项样式。有边距,文字大小,......

其他提示

为死灵道歉,但我无法在任何有关偏好样式的问题上找到答案。我终于找到了答案:默认首选项现在使用 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>

我已经设法通过将当前自定义首选项的布局资源替换为标准首选项(例如 EditTextPreference)使用的布局资源来解决此问题。这是一个代码示例,请注意 TimePreference 是自定义首选项。

    TimePreference wake_time = (TimePreference)findPreference("wake_time");
    EditTextPreference exercise = (EditTextPreference)findPreference("exercise");
    int r = exercise.getLayoutResource();
    wake_time.setLayoutResource(r);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top