【问题标题】:Preferences of varying height within a PreferenceActivityPreferenceActivity 中不同高度的首选项
【发布时间】:2012-01-06 19:01:12
【问题描述】:

我有一个自定义类,它扩展了我与 PreferenceActivity 一起使用的 Preference。

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

有没有办法用不同的 layout_height 呈现给定的偏好。

我查看了与首选项相关的 API 演示,但没有看到任何与我正在尝试做的事情相匹配的内容。

【问题讨论】:

  • 在 Api 级别 9 设备上查看的首选项不是相同的高度,而是基于内容的大小。对于 api 7 设备,我必须进行更改以应对 > 2 行的摘要。这是你的问题吗?

标签: android preferenceactivity preference


【解决方案1】:

您可以在首选项中覆盖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,否则您可能会遇到类转换异常。

【讨论】:

  • 它有效。但是,您会丢失微妙的复选框动画。
【解决方案2】:

这应该会有所帮助:

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

另一个可以被覆盖的样式属性可以在这里找到preference item layout

原答案https://stackoverflow.com/a/27027982/975886

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
  • @Mathias 只是为了避免重复
猜你喜欢
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-23
  • 2011-03-30
相关资源
最近更新 更多