我正在尝试制作一个复合控件,并为其设置自定义属性。
控件的类:
package hu.ppke.itk.kozcs.android.activities;
import hu.ppke.itk.kozcs.android.project.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SettingBox extends RelativeLayout {
public SettingBox(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
String service = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater) getContext().getSystemService(service);
RelativeLayout layout = (RelativeLayout) li.inflate(R.layout.setting_box, this, true);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SettingBox);
String title = a.getString(R.styleable.SettingBox_title);
String description = a.getString(R.styleable.SettingBox_description);
TextView titleText = (TextView) layout.findViewById(R.id.title);
TextView descriptionText = (TextView) layout.findViewById(R.id.description);
titleText.setText(title);
descriptionText.setText(description);
a.recycle();
}
}
资源/值/attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SettingBox">
<attr name="title" format="string" />
<attr name="description" format="string" />
</declare-styleable>
</resources>
控件的布局 xml (setting_box.xml):
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toLeftOf="@+id/checkbox"
android:text="@string/use_gps"
android:textColor="@android:color/white"
android:textSize="20sp" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/title"
android:layout_marginLeft="5dp"
android:layout_toLeftOf="@+id/checkbox"
android:text="@string/gps_descr"
android:textSize="13sp" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp" />
</merge>
最后是我使用此控件的布局(settings.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/hu.ppke.itk.kozcs.android.activities"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<hu.ppke.itk.kozcs.android.activities.SettingBox
andrid:id="@+id/test_setting_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:description="description"
app:title="title" />
</LinearLayout>
每次我保存最后一个 xml 时,我都会收到以下错误消息:
settings.xml:9: error: Error parsing XML: unbound prefix
日志中也有错误:
hu.ppke.itk.kozcs.android.activities.SettingBox failed to instantiate.
我在这里想念什么?