我有一个自定义类,它扩展了我与 PreferenceActivity 一起使用的 Preference。
当我尝试调整我的首选项使用的布局中的高度时(使用静态 layout_height 或使用 wrap_content),它始终显示在首选项活动中的统一高度单元格中 - 与所有“正常”首选项默认的大小相同.
有没有办法以不同的 layout_height 呈现给定的偏好。
我查看了与首选项相关的 API 演示,但没有看到任何与我正在尝试做的事情相匹配的内容。
我有一个自定义类,它扩展了我与 PreferenceActivity 一起使用的 Preference。
当我尝试调整我的首选项使用的布局中的高度时(使用静态 layout_height 或使用 wrap_content),它始终显示在首选项活动中的统一高度单元格中 - 与所有“正常”首选项默认的大小相同.
有没有办法以不同的 layout_height 呈现给定的偏好。
我查看了与首选项相关的 API 演示,但没有看到任何与我正在尝试做的事情相匹配的内容。
您可以在您的首选项中覆盖getView(View, ViewGroup)
。然后发送new LayoutParams
到getView()
. 我使用自定义的 CheckBoxPreference 进行了尝试。效果很好。
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;
}
}
请注意为视图使用正确的 LayoutParams,否则您可能会遇到类转换异常。
这应该有助于:
<!-- Application theme -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Min item height -->
<item name="android:listPreferredItemHeight">10dp</item>
</style>
可以在此处找到可以覆盖的另一个样式属性首选项布局