【问题标题】:Custom view in xml layoutxml布局中的自定义视图
【发布时间】:2011-03-16 17:01:15
【问题描述】:

我通过创建 SurfaceView 类的子类来创建自己的视图。

但是我不知道如何从 xml 布局文件中添加它。我当前的 main.xml 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<View
    class="com.chainparticles.ChainView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" 
    />


</LinearLayout>

我错过了什么?

编辑

更多信息

我的视图是这样的

package com.chainparticles;
public class ChainView extends SurfaceView implements SurfaceHolder.Callback {
    public ChainView(Context context) {
        super(context);
        getHolder().addCallback(this);
    }
// Other stuff
}

它可以像这样正常工作:

ChainView cview = new ChainView(this);
setContentView(cview);

但是尝试从 xml 中使用它时没有任何反应。

【问题讨论】:

    标签: android xml layout


    【解决方案1】:

    你想要:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    >
    
        <com.chainparticles.ChainView
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" 
         />
    </LinearLayout>
    

    编辑:

    看到你的其余代码后,它可能会抛出,因为你不能在构造函数中调用 getHolder 而被膨胀。将其移至View#onFinishInflate

    所以:

    @Override
    protected void onFinishInflate() {
        getHolder().addCallback(this);
    }
    

    如果这不起作用,请尝试将其放入您在Activitys onCreate 中调用 setContentView 之后的 init 函数中。

    它可能以前工作过,因为当从 xml 膨胀构造函数时: 调用 View(Context, AttributeSet) 而不是 View(Context)。

    【讨论】:

    • 使用我的第一个布局时,我刚刚收到黑屏,因此应用程序崩溃了。
    • 也许这些额外的建议会有所帮助。
    • 我明白了!关键是xml用Context和AttributeSet调用了构造函数(如你所说),所以添加构造函数解决了它。非常感谢!
    【解决方案2】:

    您在示例中遗漏的是标签名称,它应该是“view”(第一个非大写字母)而不是“View”。虽然大多数时候你可以把你的类名作为标签名,但如果你的类是内部类,这是不可能的,因为在Java中用来引用内部类的“$”符号在XML标签中是有限的。 所以,如果你想在你的 XML 中使用内部类,你应该这样写:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    >
    
        <view
          class="com.chainparticles.Foo$InnerClassChainView"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" 
         />
    </LinearLayout>
    

    问题是模式中同时存在“view”和“View”标签。 “View”标签(以大写字母开头)会生成一个View类,而“view”标签在解析时会检查类属性。

    【讨论】:

    • @SamusArin +1 爱他哈哈
    • 记得添加所有的构造函数。如果要覆盖 View,则有 3 个构造函数:View(Context context)、View(Context context, AttributeSet attrs) 和 View(Context context, AttributeSet attrs, int defStyleAttr)
    • 真棒答案.. +1 查看和查看标签差异
    猜你喜欢
    • 2012-07-16
    • 2012-07-01
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多