ImageButton と TextView を含むカスタム ビューを作成しました。これは私のレイアウトです:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton android:id="@+id/image_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:layout_margin="1dp" />
</merge>
CustomImageButton という RelativeLayout を継承するクラスもあります。これはコードの一部です:
public class CustomImageButton extends RelativeLayout {
...
public CustomImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
this.attributes = context.obtainStyledAttributes(attrs, R.styleable.CustomImageButton);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
((Activity)getContext()).getLayoutInflater().inflate(R.layout.custom_image_button, this);
this.setClickable(false);
setupViewItems();
}
private void setupViewItems() {
// DO STUFF HERE TO THE IMAGE BUTTON AND TEXT VIEW
// FROM THE CUSTOM ATTRIBUTES
}
}
これらは、カスタム イメージ ボタンに指定されたカスタム属性です: (attrs.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomImageButton">
<attr name="image_src" format="integer" />
<attr name="text" format="string" />
<attr name="text_size" format="dimension" />
<attr name="text_top_padding" format="integer" />
<attr name="text_alginment">
<enum name="left" value="1"/>
<enum name="right" value="2"/>
<enum name="center" value="3"/>
</attr>
<attr name="text_color" format="color"/>
</declare-styleable>
</resources>
最後に、これがメインのレイアウトです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="3" xmlns:app="http://schemas.android.com/apk/res/com.company.product.sp">
<View
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4" >
<View
android:layout_width="0px"
android:layout_height="2px"
android:layout_weight="1" />
<com.company.product.ui.views.CustomImageButton
android:id="@id/main_command"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:image_src="@drawable/main_activity"
app:text="@string/item_title"
app:text_size="@dimen/main_menu_item_text"
app:text_top_padding="55"
app:text_alginment="center"
app:text_color="@android:color/black" />
</LinearLayout>
</LinearLayout>
そして今、問題のために。Android 4.0.2以降でアプリケーションを実行すると、すべて問題ありませんが、Android 2.2.1でアプリを実行すると、
android.view.InflateException: Binary XML file line #5: Error inflating class <unknown>
スタック トレース (これは非常に有益ではありません) によると、メソッドを呼び出すと、メソッドCustomImageButton
内のクラスから例外がスローされます。なぜ、どうすればこれを修正できますか?onFinishInflate
inflate