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 に答える