2011-11-02 16 views
6

Ho visto qualcosa di simile:Il campo Riassunto della PreferenceCategory tag

<PreferenceCategory xmlns:android="http://schemas.android.com/apk/res/android" 
    android:key="vegi_category" android:title="Vegetables" 

    android:summary="Preferences related to vegetable"> <!-- Why is this here? --> 

    <CheckBoxPreference android:key="tomato_selection_pref" 
     android:title="Tomato " android:summary="It's actually a fruit" /> 
    <CheckBoxPreference android:key="potato_selection_pref" 
     android:title="Potato" android:summary="My favorite vegetable" /> 
</PreferenceCategory> 

Ma io non capisco perché c'è un campo Riassunto per la categoria pref:

(android:summary="Preferences related to vegetable")?

Quando utilizzo la schermata di pref, il riepilogo viene presentato nella vista, ma questo non è il caso della categoria pref. L'esistenza del sommario nella categoria pref è solo una convenzione di esso può essere visto in qualche modo?

Qual è l'utilizzo effettivo del sommario nell'elemento di categoria pref?

risposta

9

Il widget PreferenceCategory standard mostra solo il titolo; l'attributo android:summary viene ignorato.

Questo perché il layout di default (preference_category.xml) contiene solo un singolo TextView per il campo titolo:

<!-- Layout used for PreferenceCategory in a PreferenceActivity. --> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    style="?android:attr/listSeparatorTextViewStyle" 
    android:id="@+android:id/title" 
/> 

Se si desidera visualizzare il riepilogo pure, è possibile specificare il proprio layout utilizzando l'attributo android:layout. Per esempio:

<PreferenceCategory android:title="Category" android:summary="This is the summary" 
        android:layout="@layout/preference_category_summary"> 

Dove layout/preference_category_summary.xml è qualcosa di simile:

<!-- Layout used for PreferenceCategory + SUMMARY in a PreferenceActivity. --> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" android:layout_height="wrap_content" 
       android:orientation="vertical"> 
    <TextView android:id="@+android:id/title" 
       style="?android:attr/listSeparatorTextViewStyle"/> 
    <TextView android:id="@+android:id/summary" 
       android:paddingLeft="5dip" android:paddingRight="dip" 
       android:layout_width="match_parent" android:layout_height="wrap_content"/> 
</LinearLayout> 

Prima di andare avanti e fare questo, però, si dovrebbe considerare se è più o meno confusione per l'utente. A meno che non si modifichi attentamente il testo riassuntivo, salterà fuori dallo schermo o apparirà come allegato alla prima preferenza nella categoria.

7

Si potrebbe semplicemente usare Preference e Android: riepilogo.

<PreferenceCategory xmlns:android="http://schemas.android.com/apk/res/android" 
    android:key="vegi_category" android:title="Vegetables"> 

    <Preference android:summary="Preferences related to vegetable" /> 

    <CheckBoxPreference android:key="tomato_selection_pref" 
     android:title="Tomato " android:summary="It's actually a fruit" /> 
    <CheckBoxPreference android:key="potato_selection_pref" 
     android:title="Potato" android:summary="My favorite vegetable" /> 
</PreferenceCategory> 
+0

questo funziona. ma prestazioni non molto buone –

+0

Trovo che questo funzioni molto bene (rispetto alla modifica del layout, dal momento che deve corrispondere anche all'aspetto della versione di Android). Inoltre, se si aggiunge "android: selezionabile = falso", è possibile ottenere un aspetto simile. –

0

@ehartwell è assolutamente giusto
mio layout per le versioni diff sono:

per la pre-lollipop

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView android:id="@android:id/title" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="6dp" 
    android:textSize="14sp" 
    android:textStyle="bold" 
    android:textAllCaps="true" 
    android:paddingLeft="8dp" 
    android:paddingRight="8dp"/> 

    <TextView android:id="@android:id/summary" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="-5dp" 
    android:textSize="12sp" 
    style="?android:attr/listSeparatorTextViewStyle"/> 

</LinearLayout> 

per la post-lollipop

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingLeft="@dimen/preference_category_margin" 
    android:paddingRight="@dimen/preference_category_margin" 
    android:orientation="vertical"> 

    <TextView android:id="@android:id/title" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="6dp" 
    android:textStyle="bold" 
    android:textSize="13sp" 
    android:textAllCaps="false" 
    android:textColor="@color/colorAccent"/> 

    <TextView android:id="@android:id/summary" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="12sp" 
    android:textColor="@color/colorAccent" 
    android:textAllCaps="false"/> 

</LinearLayout> 

@ dimen/preference_category_margin è diff per le dimensioni dello schermo speciale
16dp o 24dp
e sono abbastanza bene :)