我已经扩展了 aTextView
来实现Checkable
. 我没有碰过任何TextView
东西,除了onCreateDrawableState
. 此外,我刚刚实现了 Checkable 方法。(为清楚起见进行了删节。)
public class CheckableTextView extends TextView implements Checkable {
public CheckableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CheckedStateSet);
}
return drawableState;
}
它在 ICS 上编译得很好,但在 Android 2.3 中,当程序试图放大这个视图时,我得到了一个非常奇怪的错误。
10-31 00:51:20.414: E/AndroidRuntime(601): FATAL EXCEPTION: main
10-31 00:51:20.414: E/AndroidRuntime(601): android.view.InflateException: Binary XML file line #3: Error inflating class net.blackenvelope.utrechtafval.map.layouts.CheckableTextView
...
10-31 00:51:20.414: E/AndroidRuntime(601): Caused by: java.lang.reflect.InvocationTargetException
10-31 00:51:20.414: E/AndroidRuntime(601): at java.lang.reflect.Constructor.constructNative(Native Method)
10-31 00:51:20.414: E/AndroidRuntime(601): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
10-31 00:51:20.414: E/AndroidRuntime(601): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
10-31 00:51:20.414: E/AndroidRuntime(601): ... 45 more
10-31 00:51:20.414: E/AndroidRuntime(601): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2
10-31 00:51:20.414: E/AndroidRuntime(601): at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
10-31 00:51:20.414: E/AndroidRuntime(601): at android.view.View.<init>(View.java:1963)
10-31 00:51:20.414: E/AndroidRuntime(601): at android.widget.TextView.<init>(TextView.java:344)
10-31 00:51:20.414: E/AndroidRuntime(601): at android.widget.TextView.<init>(TextView.java:337)
10-31 00:51:20.414: E/AndroidRuntime(601): at net.app.layouts.CheckableTextView.<init>(CheckableTextView.java:11)
10-31 00:51:20.414: E/AndroidRuntime(601): ... 48 more
我似乎无法找出这种 type=0x2 的含义。
这是 XML:
<?xml version="1.0" encoding="utf-8"?>
<app.CheckableTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/overlay_type_title"
android:layout_width="match_parent"
android:background="@drawable/overlay_list_bg_selector"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/overlay_list_icon"
android:drawablePadding="10dp"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingLeft="10dp"
android:paddingRight="?android:attr/listPreferredItemPaddingRight"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
>
</app.CheckableTextView>