2010-11-24 8 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視圖並將其視圖屬性調整爲您的視圖屬性。

相關問題