我正在使用此 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的回答,我从 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);
}