我想定义一个自定义小部件,它包含一些其他控件并有自己的逻辑。我想使用XML文件来定义UI,以及Java代码来定义逻辑。但我不知道怎么做。
我看了这篇文章,但没用。下面是我的代码。
XML:

<com.example.MyView xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="custom button 1"/>

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="custom button 2"/>

</com.example.MyView>

Java代码:
public class MyView extends LinearLayout {

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        LayoutInflater.from(getContext()).inflate(R.layout.test_view, this);  // **line 32**
    }
}

在主布局中,我这样调用它:
<com.example.MyView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
</com.example.MyView>

不幸的是,它在运行时引发异常:
05-26 14:11:25.199: ERROR/AndroidRuntime(15799): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MyActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
    at android.view.LayoutInflater.createView(LayoutInflater.java:518)
    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    at com.example.MyView.onFinishInflate(MyView.java:32)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:631)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    at com.example.MyView.onFinishInflate(MyView.java:32)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:631)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    at com.example.MyView.onFinishInflate(MyView.java:32)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:631)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    at com.example.MyView.onFinishInflate(MyView.java:32)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:631)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    ....

最佳答案

不能像以前那样声明自定义视图。如果在主布局中使用自定义视图,将调用onFinishInflate方法来构造自定义视图,并在该方法中调用上面的布局。问题是,您已经在该布局中拥有一个自定义视图引用(当LayoutInflater遇到自定义视图标记时,该引用将再次膨胀),因此您将处于循环膨胀状态。
你可能想要这样的东西:

<merge xmlns:android="http://schemas.android.com/apk/res/android>

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="custom button 1"/>

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="custom button 2"/>

</merge>

因此,当您的自定义视图将从布局文件中展开时,它将附加上面的xml布局的内容。onFinishInflate方法将保持不变:
@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    LayoutInflater.from(getContext()).inflate(R.layout.test_view, this);  // **line 32**
}

编辑:在onFinishInflate方法中膨胀内容布局似乎再次触发该方法(至少在旧版本上)。您可以修改布局以使用如下LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="custom button 1"/>

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="custom button 2"/>

</LinearLayout>

然后在onFinishInflate方法中:
@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    View v = LayoutInflater.from(getContext()).inflate(R.layout.aaaaaaaaaaa, this, false);
    addView(v);
}

这将在布局层次结构中引入另一个无用的元素,因此您只需在自定义视图的构造函数中使用merge标记对布局进行充气即可:
public MyView(Context context) {
    super(context);
    LayoutInflater.from(getContext()).inflate(R.layout.test_view, this);
}

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater.from(getContext()).inflate(R.layout.test_view, this);
}

public MyView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    LayoutInflater.from(getContext()).inflate(R.layout.test_view, this);
}

07-28 02:31
查看更多