6

Je l'ai vu quelque chose comme ceci:Le champ résumé de la balise PreferenceCategory

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

Mais je ne comprends pas pourquoi est-il un champ de synthèse pour la catégorie pref:

(android:summary="Preferences related to vegetable")? Lorsque j'utilise l'écran pref, le résumé est présenté dans la vue, mais ce n'est pas le cas avec la catégorie pref. Est-ce que l'existence de résumé dans la catégorie pref peut-elle être vue d'une façon ou d'une autre?

Quelle est l'utilisation réelle du résumé dans l'élément pref category?

Répondre

9

Le widget standard PreferenceCategory n'affiche que le titre; l'attribut android:summary est ignoré.

C'est parce que la mise en page par défaut (preference_category.xml) ne contient qu'un seul TextView pour le champ titre:

<!-- 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" 
/> 

Si vous voulez montrer le résumé ainsi, vous pouvez spécifier votre propre mise en page en utilisant l'attribut android:layout. Par exemple:

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

Lorsque la mise en page/preference_category_summary.xml est quelque chose comme:

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

Avant d'aller de l'avant et cela, cependant, vous devriez considérer si elle est moins ou plus déroutant pour l'utilisateur. À moins que vous ne stylisiez le texte résumé avec soin, il sautera hors de l'écran ou apparaîtra attaché à la première préférence de la catégorie.

7

Vous pourriez simplement utiliser la préférence et Android: résumé.

<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

cela fonctionne. mais pas très bonne performance –

+0

Je trouve que cela fonctionne très bien (par rapport à la modification de la mise en page, car il doit correspondre à l'aspect et la convivialité de la version Android). En outre, si vous ajoutez 'android: selectable = false', vous pouvez obtenir un aspect similaire. –

0

@ehartwell est tout à fait raison
mes mises en page pour les versions diff sont:

pour le pré-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> 

pour 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 est diff pour la taille de l'écran spécial
16DP ou 24dp
et ils sont assez bien :)

Questions connexes