Android: стиль предпочтений - PullRequest
       24

Android: стиль предпочтений

5 голосов
/ 28 сентября 2010

Я сделал пользовательское предпочтение (то есть предпочтение с пользовательским макетом), которое отображается в списке предпочтений PreferenceActivity.

Макет создан в коде. Проблема в том, что шрифт TextView, созданный в коде, выглядит несколько иначе, чем стандартный предпочтительный шрифт Android.

Таким образом, решение было бы применить атрибуты стиля предпочтения Android к моему TextView. Соответствующие стили должны быть preferenceScreenStyle или preferenceStyle (я не уверен).

Моя проблема в том, что я не могу понять, как считывать стандартные атрибуты стиля Android, поэтому я могу установить их в коде.

Ответы [ 3 ]

6 голосов
/ 22 декабря 2010

У меня такая же проблема, но я исправил ее для некоторых мобильных устройств, HTC Saphire и Samsung Galaxy S, но у меня проблемы с моим HTC Desire HD.Вы можете увидеть стандартный стиль предпочтений в android_SDK_resurces / layout / preference.xml.Есть поля, размеры текста, ....

3 голосов
/ 17 октября 2016

Извинения за некро, но я не смог найти этот ответ ни на один 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 голос
/ 27 января 2013

Мне удалось решить эту проблему, заменив ресурс макета текущего пользовательского предпочтения ресурсом макета, используемым стандартным предпочтением, таким как EditTextPreference, например, Вот пример кода, обратите внимание, что TimePreference является пользовательской настройкой.

    TimePreference wake_time = (TimePreference)findPreference("wake_time");
    EditTextPreference exercise = (EditTextPreference)findPreference("exercise");
    int r = exercise.getLayoutResource();
    wake_time.setLayoutResource(r);
...