不同高度的内preferenceActivity preferences高度、不同、preferences、preferenceActivity

2023-09-08 08:59:24 作者:浅眉

我有一个扩展,我使用联同一个preferenceActivity preference自定义类。

I have a custom class that extends Preference that I'm using in conjunction with a PreferenceActivity.

当我试图调节我的preference使用(用静态layout_height或WRAP_CONTENT)布局的高度,它总是显示在preference活动一致的高度细胞 - 同样大小所有的正常preferences默认的

When I try to adjust the height in the layout my Preference is using (with a static layout_height or with wrap_content) it is always displayed in a uniform height cell in the Preference Activity - the same size that all of the "normal" preferences default to.

有没有一种方法present一个给定的preference用不同的layout_height。

Is there a way present a given preference with a different layout_height.

我看了相关的preferences API的演示,我没有看到任何与我想要做的事情。

I've looked at the API demos related to preferences and I'm not seeing anything that matches what I'm trying to do.

推荐答案

您可以在preference覆盖getView(查看,ViewGroup中)。然后发送新的LayoutParams到视图。我试图用一个自定义的复选框preference。伟大工程。

You can override getView(View, ViewGroup) in your Preference. Then send new LayoutParams to the view. 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 hieght = android.view.ViewGroup.LayoutParams.MATCH_PARENT;
    final int width = 300;
    final LayoutParams params = new LayoutParams(hieght, width);
    v.setLayoutParams(params );
    return v;
}

}

只是要小心使用正确的LayoutParams的视图,或者你可能会得到一类转换异常。

Just be careful to use the correct LayoutParams for the View or you might get a class cast exception.