2010-09-28 13 views
5

He hecho una preferencia personalizada (es decir, una preferencia con un diseño personalizado) que se muestra en la lista de preferencias de PreferenceActivity.Android: Estilo de preferencia

El diseño se crea en código. El problema es que la fuente del TextView creado en el código se ve algo diferente a la fuente de preferencia estándar de Android.

Así que la solución sería aplicar los atributos de estilo de la preferencia de android a mi TextView. Los estilos respectivos deben ser preferenceScreenStyle o preferenceStyle (no estoy seguro).

Mi problema es que no puedo entender cómo leer los atributos de estilo estándar de Android, para poder configurarlos en el código.

Respuesta

6

Tengo el mismo problema pero lo arreglé para algunos dispositivos móviles, HTC Saphire y Samsung Galaxy S, pero tengo problemas con mi HTC Desire HD. Puede ver el estilo de preferencia estándar en android_SDK_resurces/layout/preference.xml. Existen los márgenes, tamaños de texto, ...

1

He logrado resolver esto reemplazando el recurso de diseño de las preferencias personalizadas actuales con el recurso de diseño utilizado por una preferencia estándar como EditTextPreference, p. Aquí hay un ejemplo de código, tenga en cuenta que TimePreference es la preferencia personalizada.

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

Disculpas por el necro pero no he podido encontrar esta respuesta en ninguna pregunta de SO sobre el estilo de preferencia. Finalmente encontré la respuesta: la preferencia predeterminada ahora usa el layout/preference_material. Puede verlo y otros diseños más específicos en la fuente de Android here. Copie a continuación solo en caso de que el enlace se rompa:

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