2010-11-24 8 views
5

私はAndroidアプリケーション用のxmlの設定画面を書いています。私の問題は、プリファレンスのタイトルのうちのいくつかが長すぎてページをラップしないことです。私の例を考えてみましょう:CheckBox Titleのサイズを変更する方法、または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

A CheckBoxPreference :)

おかげでビューから派生されていないので、通常のビューの属性が適用されません。

代わりに、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="..." ... /> 
+0

私はdonot結果を得る、私はまた1行を得る – pengwang

+0

申し訳ありませんが、私の記憶は私に少し間違っています。適所での編集。 TextViews(CheckBoxではなく)を探し、単一行フラグをオフにして、Ellipsizeをnullに設定します。要約ではなくタイトルにのみ適用する場合は、「改行」も挿入します。ヘッダーが最初に来るからです。これは、Androidフレームワークが将来のリリースでチェックボックスの設定のレイアウトを変更すると、効果がなくなる可能性があるため、これはハックの一種であることに注意してください。しかし、常にCheckboxプリファレンスのすべてのテキストビューには、提供されたすべてのテキストが表示されます。通常はOKであるはずです。 – Thorstenvv

+0

私はまた結果を得ることができません。 – pengwang

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を上書きすることができます。クラスonBindViewCheckBoxビューを見つけてビュー属性を自分のものに合わせます。

+0

[@ Thorstenwの答え](http://stackoverflow.com/a/4268968/101361)から少し修正した段落をコピーすると、この質問に付加的な価値がもたらされますか? – laalto

関連する問題