gpt4 book ai didi

android - PreferenceActivity 中不同高度的偏好

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:33:50 36 4
gpt4 key购买 nike

我有一个扩展 Preference 的自定义类,我将它与 PreferenceActivity 结合使用。

当我尝试调整我的 Preference 正在使用的布局中的高度(使用静态 layout_height 或 wrap_content)时,它始终显示在 Preference Activity 中的统一高度单元格中 - 与所有“正常”的尺寸相同首选项默认为。

有没有办法用不同的 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;
}

请注意为 View 使用正确的 LayoutParams,否则您可能会遇到类转换异常。

关于android - PreferenceActivity 中不同高度的偏好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8762996/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com