2

この CheckBoxPreference クラスを使用して、Android 4.2 より前のバージョンの RTL テキスト方向をサポートし、好みのカスタム フォント スタイルを使用しています。

public class MyCheckBoxPreference extends CheckBoxPreference {


   TextView txtTitle, txtSum;
   View Custmv;


   public MyCheckBoxPreference(Context context) {
    super(context);
}

   public MyCheckBoxPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
}

   public MyCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

   @Override
   protected void onBindView(@NonNull View view) {
    super.onBindView(view);
       Context context = getContext();
       txtTitle = (TextView) view.findViewById(android.R.id.title);
       txtTitle.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf"));
       txtSum = (TextView) view.findViewById(android.R.id.summary);
       txtSum.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf"));
}

   @Override
   protected View onCreateView(ViewGroup parent) {
       Context ccc = getContext();
       LayoutInflater inflater = (LayoutInflater) ccc.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       Custmv = inflater.inflate(R.layout.arabic_checkboxpreference_layout, parent, false);
    return Custmv;
}

私はprefs.xmlで次のように使用しています。

<com.app.myclasses.MyCheckBoxPreference
        android:defaultValue="false"
        android:key="cb_big"
        android:summaryOff="@string/off"
        android:summaryOn="@string/on"
        android:title="@string/Show_Big_Text" />

そして、ここにarabic_checkboxpreference_layout.xmlがあります:

<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:paddingRight="?android:attr/scrollbarSize"
android:id="@+id/sos">

<CheckBox
    android:id="@+android:id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_marginLeft="16dip"
    android:minWidth="56dp"
    android:gravity="center"
    android:layout_gravity="center" />

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

    <TextView
        android:id="@+android:id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceMedium"

        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:textColor="?android:attr/textColorPrimary"
        android:text="Title"
        android:layout_alignParentRight="true" />

    <TextView
        android:id="@android:id/summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@android:id/title"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:maxLines="3"
        android:text="subTitle"
        android:layout_alignRight="@android:id/title"
        android:textColor="?android:attr/textColorSecondary" />

</RelativeLayout>

すべてがうまく機能しますが、問題は、カスタム クラスの複数のインスタンスを使用する場合です。つまり、prefs.xml で複数の CheckBoxPreferences を使用し、CheckBoxPreference をクリックすると、しばらくの間 (約 1 ~ 2 秒) フォーカスされます。チェックを入れたり外したり。何かアイデアはありますか?

ここに画像の説明を入力

編集

kha answer によると、onBind メソッドからコンストラクターへの初期化を削除したところ、すべてがスムーズになりました。

public MyCheckBoxPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.mTypeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf");
    this.Custmv = View.inflate(context, R.layout.arabic_checkboxpreference_layout, null);
    TextView txtTitle = (TextView) Custmv.findViewById(android.R.id.title);
    txtTitle.setTypeface(mTypeface);
    TextView txtSum = (TextView) Custmv.findViewById(android.R.id.summary);
    txtSum.setTypeface(mTypeface);
}
4

1 に答える 1

0

Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf")ビューをバインドするたびにフォントをロードするという事実に関連している可能性があります。

メソッドから移動して、onBindView代わりにビューで一度作成し、フォントを設定する必要があるときに参照を使用して、それが違いを生むかどうかを確認してください

于 2015-04-03T08:58:42.563 に答える