我正在使用此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);
}
答案 0 :(得分:0)
这可能与您每次绑定视图时加载Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf")
字体的事实有关。
尝试将其从onBindView
方法移出并在视图中创建一次,并在需要设置字体时使用参考,看看是否有任何区别