Preferences of varying height within a PreferenceActivity

给你一囗甜甜゛ 提交于 2019-12-01 17:43:53
theJosh

You can override getView(View, ViewGroup) in your Preference. Then send new LayoutParams to the getView(). I tried it with a customized CheckBoxPreference. 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 might get a class cast exception.

Nikolay Nikiforchuk

This should help:

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

another styling attributes that can be overridden can be found here preference item layout

Original answer https://stackoverflow.com/a/27027982/975886

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!