Pergunta

Eu fiz uma preferência personalizada (ou seja, uma preferência com um layout personalizado) que exibia na lista de preferências de um PreferenceActivity.

O layout é criado no código. O problema é que a fonte do TextView criada no código parece um pouco diferente da fonte de preferência padrão do Android.

Portanto, a solução seria aplicar os atributos de estilo da preferência do Android ao meu TextView. Os respectivos estilos devem ser PreferênciasCreenStyle ou preferenchestyle (Não tenho certeza).

Meu problema é que não consigo descobrir como ler os atributos de estilo padrão do Android, para que eu possa defini -los no código.

Foi útil?

Solução

Tenho o mesmo problema, mas eu o corrigi para alguns dispositivos móveis, HTC Saphire e Samsung Galaxy S, mas tenho problemas com o meu HTC Desire HD. Você pode ver o estilo de preferência padrão em android_sdk_resurces/layout/preference.xml. Existem as margens, tamanhos de texto, ....

Outras dicas

Desculpas pelo Necro, mas não consegui encontrar essa resposta em nenhuma pergunta sobre o estilo de preferência. Finalmente encontrei a resposta: a preferência padrão agora usa o layout/preference_material. Você pode ver e outros layouts mais específicos na fonte do Android aqui. Copiado abaixo, caso o link quebre:

<?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>

Consegui resolver isso substituindo o recurso de layout da preferência personalizada atual pelo recurso de layout usado por uma preferência padrão como o EdittExtPreference, por exemplo, aqui está um exemplo de código, observe que o tempo de tempo é a preferência personalizada.

    TimePreference wake_time = (TimePreference)findPreference("wake_time");
    EditTextPreference exercise = (EditTextPreference)findPreference("exercise");
    int r = exercise.getLayoutResource();
    wake_time.setLayoutResource(r);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top