2010-11-24 26 views
5

我正在为我的Android应用程序在xml中编写一个首选项屏幕。我的问题是,偏好的一些标题太长,不会包装页面。考虑我的例子:如何更改CheckBox标题的大小或将它放在PreferenceScreen 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标题的大小吗?

任何帮助将不胜感激。

谢谢:)

回答

11

一个CheckBoxPreference不是从查看得到的,所以通常的看法属性不适用。

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

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

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="..." ... /> 
+0

我没有得到结果,我也得到一行 – pengwang 2010-11-25 14:46:37

+0

对不起,我的记忆是服务我有点错了。编辑到位。寻找TextViews(不是CheckBox),只需关闭单行标志并设置Ellipsize为空。如果您只想将此应用于标题而不是摘要,请插入“中断”。因为标题首先出现。应该指出的是,这是一种黑客行为,因为如果Android框架在未来版本中更改复选框首选项的布局,它可能会变得无效。但是,它将始终使复选框首选项中的所有文本显示提供的所有文本都应该正常。 – Thorstenvv 2010-11-26 17:14:26

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”“。

0

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

相关问题