Summary field for the PreferenceCategory tag

I saw something like this:

<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 actually a fruit" /> <CheckBoxPreference android:key="potato_selection_pref" android:title="Potato" android:summary="My favorite vegetable" /> </PreferenceCategory> 

But I do not understand why there is a resume field for the pref category:

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

When I use the pref screen, the summary is presented in the view, but this does not belong to the pref category. Is the existence of summary information in the prefix category only its convention, can you somehow see?

What is the actual use of the resume in the pref category element?

+6
source share
3 answers

The standard PreferenceCategory widget shows only the title; The android:summary attribute is ignored.

This is because the default layout ( preference_category.xml ) contains only one TextView for the header field:

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

If you want to also show a summary, you can specify your own layout using the android:layout attribute. For instance:

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

Where layout / preference_category_summary.xml is something like:

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

Before you go further and do this, you should think about how less or less confusing for the user. If you do not accurately draw the final text, it either jumps out of the screen, or seems to join the first preference in the category.

+9
source

You can simply use Preference and android: summary.

 <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 actually a fruit" /> <CheckBoxPreference android:key="potato_selection_pref" android:title="Potato" android:summary="My favorite vegetable" /> </PreferenceCategory> 
+7
source

@ehartwell is absolutely right
my layouts for diff versions:

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

for 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 is diff for custom screen size
16dp or 24dp
and they are pretty good :)

0
source

Source: https://habr.com/ru/post/900592/


All Articles