我想创建一个具有以下布局的小部件:
<LinearLayout orientation="horizontal">
<CheckBox />
<EditText />
<SeekBar />
</LinearLayout>
当搜索栏被移动时,它应该选中复选框并使用当前搜索栏值更新编辑框。
我在应用程序中一遍又一遍地使用类似的东西,并且厌倦了重新实现相同的东西。
将所有这些封装到自己的类中以供重用的最佳方法是什么?
public class SeekChoice extends LinearLayout {
public SeekChoice(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
LinearLayout ll = (LinearLayout) inflater.inflate(R.id.widget_item, null);
this.addView(ll);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="@+id/widget_item"
>
<CheckBox
android:id="@+id/checkBox_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item Name" />
<EditText
android:id="@+id/editText_item_count"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:enabled="false"
android:ems="10" >
</EditText>
<SeekBar
android:id="@+id/seekBar_item_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
然后在我的布局中我放了:
<com.example.lobsternav.widget.SeekChoice
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
例外情况如下:
09-09 20:23:08.759: E/AndroidRuntime(21888): FATAL EXCEPTION: main
09-09 20:23:08.759: E/AndroidRuntime(21888): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lobsternav/com.example.lobsternav.MarkActivity}: android.view.InflateException: Binary XML file line #344: Error inflating class com.example.lobsternav.widget.SeekChoice
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.os.Looper.loop(Looper.java:137)
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-09 20:23:08.759: E/AndroidRuntime(21888): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 20:23:08.759: E/AndroidRuntime(21888): at java.lang.reflect.Method.invoke(Method.java:511)
09-09 20:23:08.759: E/AndroidRuntime(21888): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-09 20:23:08.759: E/AndroidRuntime(21888): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-09 20:23:08.759: E/AndroidRuntime(21888): at dalvik.system.NativeStart.main(Native Method)
09-09 20:23:08.759: E/AndroidRuntime(21888): Caused by: android.view.InflateException: Binary XML file line #344: Error inflating class com.example.lobsternav.widget.SeekChoice
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.view.LayoutInflater.createView(LayoutInflater.java:596)
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-09 20:23:08.759: E/AndroidRuntime(21888): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.app.Activity.setContentView(Activity.java:1867)
09-09 20:23:08.759: E/AndroidRuntime(21888): at com.example.lobsternav.MarkActivity.onCreate(MarkActivity.java:85)
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.app.Activity.performCreate(Activity.java:5008)
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-09 20:23:08.759: E/AndroidRuntime(21888): ... 11 more
09-09 20:23:08.759: E/AndroidRuntime(21888): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
09-09 20:23:08.759: E/AndroidRuntime(21888): at java.lang.Class.getConstructorOrMethod(Class.java:460)
09-09 20:23:08.759: E/AndroidRuntime(21888): at java.lang.Class.getConstructor(Class.java:431)
09-09 20:23:08.759: E/AndroidRuntime(21888): at android.view.LayoutInflater.createView(LayoutInflater.java:561)
09-09 20:23:08.759: E/AndroidRuntime(21888): ... 24 more
疯狂......如果我没有定义以下构造函数,它会引发异常:
public SeekChoice(Context context, AttributeSet attrs) {
super(context, attrs);
}
我猜 LinearLayout 的所有构造函数都需要定义并且需要调用超级构造函数?