【问题标题】:How to define a custom widget using both XML and java code如何使用 XML 和 java 代码定义自定义小部件
【发布时间】:2012-10-14 08:08:48
【问题描述】:

我想定义一个自定义小部件,它包含一些其他控件并具有自己的逻辑。我想使用 XML 文件来定义 UI,并使用 java 代码来定义逻辑。但我不知道该怎么做。

我关注了这篇文章Building mix-up custom Android component/widget using Java class and XML layout,但没有工作。以下是我的代码。

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)
    ....

【问题讨论】:

    标签: android custom-controls


    【解决方案1】:

    您不能像以前那样声明自定义视图。如果您在主布局中使用自定义视图,则将调用 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);
    }
    

    【讨论】:

    • merge 也不起作用。错误信息是android.view.InflateException: Binary XML file line #5: Error inflating class &lt;unknown&gt;
    • 它在我的 android 4.1 pad 上工作,但不是我的 android 2.3 手机。如何让它在 android 2.3(甚至 2.2)上运行?
    • @Freewind 您是否将 android 命名空间添加到 merge 标记? &lt;merge xmlns:android="http://schemas.android.com/apk/res/android" &gt;...
    • 我做了,和你的一模一样。
    猜你喜欢
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多