【问题标题】:Android PreferenceActivity Item HeightAndroid PreferenceActivity 项高度
【发布时间】:2012-01-12 15:47:53
【问题描述】:

我有一个带有两个复选框的 PreferenceActivity。一切正常,但我遇到了 UI 问题;并非复选框的所有文本都适合复选框行。

有没有一种很好的方法来设置这些复选框首选项的最小高度而不用硬编码一个值?

编辑 - 添加 xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
   <CheckBoxPreference
      android:key="alarm_set"
      android:title="@string/set_alarm_title"
      android:defaultValue="true" />
   <CheckBoxPreference
      android:key="store_pages"
      android:title="@string/store_downloaded_pages_title"
      android:summary="@string/store_downloaded_pages"
      android:defaultValue="false" />
</PreferenceScreen>

【问题讨论】:

  • 如果您想要比以下猜测更具体的答案,请向我们展示您的 PreferenceActivity 的 XML
  • 我会将它添加到问题中,因为它比评论显示得更好
  • 是的,您必须像 iFor 显示的那样进行自定义偏好设置。您需要做的就是控制摘要文本,如果您找到源它可能设置为 maxLines 1,这就是您的文本没有显示的原因。

标签: android preferenceactivity


【解决方案1】:

基于 this 关于文本偏好的问题,我使用这个类具有相同的 iussue。

public class LongSummaryCheckBoxPreference extends CheckBoxPreference 
{ 
    public LongSummaryCheckBoxPreference(Context ctx, AttributeSet attrs, int defStyle) 
    { 
        super(ctx, attrs, defStyle);         
    } 

    public LongSummaryCheckBoxPreference(Context ctx, AttributeSet attrs) 
    { 
        super(ctx, attrs);   
    } 

    @Override 
    protected void onBindView(View view)
    {        
        super.onBindView(view); 

        TextView summary= (TextView)view.findViewById(android.R.id.summary); 
        summary.setMaxLines(4); 
    }        
} 

在 xml 中,它看起来像这样。

    <com.iforpowell.android.ipbike.LongSummaryCheckBoxPreference android:key="@string/key_distance_line_enable"
        android:title="@string/title_distance_line_enable" android:summary="@string/summary_distance_line_enable"
        android:defaultValue="true" />
    <com.iforpowell.android.ipbike.LongSummaryCheckBoxPreference android:key="@string/key_altitude_line_enable"
        android:title="@string/title_altitude_line_enable" android:summary="@string/summary_altitude_line_enable"
        android:defaultValue="true" />

我对 EditTextPreference 有完全相同的东西,它有相同的问题,即 Api 中的摘要最多 2 行

【讨论】:

  • 它仍然使用硬编码值 - setMaxLines(4)。我想可以 setMaxLines(20) 来确保永远不会有任何裁剪,但你知道是否有一种方法可以让容器“刚好适合”文本?
  • 好吧 setMaxLines 只要比以往任何时候都多,就可以满足您所需要的,您不会一直得到 4 个,最多只有 4 个。因此,如果您真的想要,请将其设置为 MAX_INT .
  • 感谢您的帮助。赏金和圣诞快乐!
【解决方案2】:

在 xml 中,您可以将高度设置为“wrap_content”。那时它将调整大小以适合您放入的任何内容。所以,大致是这样的:

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CheckBox" />

【讨论】:

    【解决方案3】:

    这应该会有所帮助

    就这样做吧:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      相关资源
      最近更新 更多