在PreferenceActivity中设置不同高度的偏好设置

7
我有一个自定义类,它扩展了Preference,并与PreferenceActivity一起使用。
当我尝试调整布局中我的偏好设置使用的高度(使用静态layout_height或wrap_content)时,它总是以统一高度单元格的形式显示在Preference Activity中 - 与所有“普通”首选项默认大小相同。
是否有一种方法来呈现具有不同layout_height的给定首选项。
我查看了与偏好设置相关的API演示,但没有看到任何与我尝试做的事情匹配的内容。

在 API 级别为 9 的设备上查看,偏好设置的高度不同,而是基于内容的大小。对于 API 级别为 7 的设备,我不得不进行更改以应对摘要超过 2 行的情况。这是你的问题吗? - Ifor
2个回答

7

您可以在自己的Preference中重写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来对视图进行布局,否则可能会出现类转换异常。


它可以工作。但是,您会失去微妙的复选框动画。 - Vikram Gupta

-1

这应该会有所帮助:

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

还有其他可以被覆盖的样式属性,可以在这里找到偏好项布局

原始答案https://dev59.com/ZXrZa4cB1Zd3GeqP0Ub4#27027982


虽然这个链接可能回答了问题,但最好在此处包含答案的基本部分并提供参考链接。仅有链接的答案如果链接页面发生更改可能会变得无效。 - Mathias
@Mathias 这只是为了避免重复。 - Nikolay Nikiforchuk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接