5

我正在为我的 Android 应用程序在 xml 中编写首选项屏幕。我的问题是首选项的某些标题太长,不会换行。考虑我的例子:

 <CheckBoxPreference
                android:title="Language Detection (BETA)"
                android:defaultValue="false"
                android:summary="Automatically decide which language to use"
                android:key="lan_det_pref"
                android:textSize="15px"
                android:lines="4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                />

也许我遗漏了一些东西,但我尝试使用许多其他属性,但没有得到积极的结果。我尝试过 android:singleLine、android:lines 等,但这些似乎都不会影响首选项的标题。还有一种方法可以缩小 CheckBox 标题的大小吗?

任何帮助将不胜感激。

谢谢 :)

4

3 回答 3

11

CheckBoxPreference 不是从 View 派生的,因此通常的视图属性不适用。

相反,CheckBoxPreference 绑定到具有预定义布局的视图。

您可以从 CheckBoxPreference 派生一个类并覆盖 onBindView。在您的班级的 onBindView 中,找到 CheckBox 视图并根据自己的喜好调整其视图属性。

class MyCBPref extends CheckBoxPreference{
  public MyCBPref( Context context, AttributeSet attrs){
    super(context, attrs);
  }
  protected void onBindView( View view){
    super.onBindView(view);
    makeMultiline(view); 
  }
  protected void makeMultiline( View view)
  {
    if ( view instanceof ViewGroup){

        ViewGroup grp=(ViewGroup)view;

        for ( int index = 0; index < grp.getChildCount(); index++)
        {
            makeMultiline(grp.getChildAt(index));
        }
    } else if (view instanceof TextView){
        TextView t = (TextView)view;
        t.setSingleLine(false); 
        t.setEllipsize(null);
    }
  }
}

然后在您的布局中,引用您的类而不是 CheckBoxPreference:

<com.mycorp.packagename.MyCBPref android:title="..." ... />
于 2010-11-24T16:17:39.650 回答
2

您可以像这样自定义您的复选框:MyPreference.xml

<CheckBoxPreference android:key="testcheckbox" 
    android:title="Checkbox Title" 
    android:summary="Checkbox Summary..." 
    android:layout="@layout/custom_checkbox_preference_layout">
</CheckBoxPreference>

和 custom_checkbox_preference_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingLeft="16dip"
    android:paddingRight="?android:attr/scrollbarSize">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="horizontal">
        <ImageView
            android:id="@+android:id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="false"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignLeft="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4" />

    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout android:id="@+android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" />

</LinearLayout>

关键是 android:singleLine="false""。

于 2012-07-06T06:57:57.877 回答
0

您可以从中派生一个类CheckBoxPreference并覆盖onBindView. 在您的班级onBindView中,找到CheckBox视图并将其视图属性调整为您的。

于 2013-05-30T07:11:30.077 回答