Preferences of different heights within the PreferenceActivity

I have a custom class that extends the Preference that I use in conjunction with PreferenceActivity.

When I try to adjust the height in a layout that uses My Preference (with static layout_height or with wrap_content), it always appears in a cell with the same height in the Activity Preference - the same size as all the "normal" preferences by default.

Is there a way to provide a given preference with another layout_height.

I looked at the API demos related to settings, and I don't see anything that matches what I'm trying to do.

+4
source share
2 answers

You can override getView(View, ViewGroup) in your preference. Then send new LayoutParams to getView() . I tried it with a custom CheckBoxPreference. It works great.

 import android.content.Context; import android.preference.CheckBoxPreference; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView.LayoutParams; public class CustomCheckBoxPreference extends CheckBoxPreference { public CustomCheckBoxPreference(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); } public CustomCheckBoxPreference(final Context context, final AttributeSet attrs) { super(context, attrs); } public CustomCheckBoxPreference(final Context context) { super(context); } @Override public View getView(final View convertView, final ViewGroup parent) { final View v = super.getView(convertView, parent); final int height = android.view.ViewGroup.LayoutParams.MATCH_PARENT; final int width = 300; final LayoutParams params = new LayoutParams(height, width); v.setLayoutParams(params ); return v; } 

}

Just be careful to use the correct LayoutParams for the view, or you may get a class exception.

+7
source

This should help:

 <!-- Application theme --> <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar"> <!-- Min item height --> <item name="android:listPreferredItemHeight">10dp</item> </style> 

other style attributes that can be overridden can be found here location of preference items

Original answer fooobar.com/questions/1389709 / ...

-1
source

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


All Articles