Android: how to create a ListView that is similar to Preferences?

I don’t want to use the provided Preferences infrastructure, instead I would like to create a ListView similar. In particular, I would like to use the same font size and style for TextViews.

+6
source share
3 answers

This is not about the ListView itself, but about the child views that appear inside the ListView. They are created using the getView method of your adapter.

To create views like Android, you can use the Android source code, in particular the corresponding layouts of the XML files. For example, preference.xml looks like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?android:attr/listPreferredItemHeight" android:gravity="center_vertical" android:paddingRight="?android:attr/scrollbarSize"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dip" android:layout_marginRight="6dip" android:layout_marginTop="6dip" android:layout_marginBottom="6dip" android:layout_weight="1"> <TextView android:id="@+android:id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:textAppearance="?android:attr/textAppearanceLarge" android:ellipsize="marquee" android:fadingEdge="horizontal" /> <TextView android:id="@+android:id/summary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@android:id/title" android:layout_alignLeft="@android:id/title" android:textAppearance="?android:attr/textAppearanceSmall" android:maxLines="4" /> </RelativeLayout> <!-- Preference should place its actual preference widget here. --> <LinearLayout android:id="@+android:id/widget_frame" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="vertical" /> </LinearLayout> 

You will not be able to directly use this content, since some of the constants used are private for Android, and you will have to go through other XML files even more.

In any case, you should keep in mind that Android preferences look different in different versions of Android and on different themes, so make sure you use the constants provided by Android and not your own hard-coded values ​​to make sure your list items are The browsers resemble the actual settings provided by Android.

+9
source

Add this attribute to the activity tag in AndroidManifest.xml

  android:theme="@android:style/Preference.PreferenceScreen" 
+1
source

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


All Articles