Android 使用XML布局作为视图子类的视图?

Android 使用XML布局作为视图子类的视图?,android,android-layout,android-view,Android,Android Layout,Android View,我觉得我以前好像知道如何做到这一点,但我现在是一片空白。我有一个从视图(Card)扩展的类,我用XML为它编写了一个布局。我要做的是将Card的视图设置为构造函数中的XML视图,这样我就可以使用Card中的方法来设置TextViews等等。有什么建议吗?代码如下: Card.java: (我有View.inflate(context,R.layout.card_layout,null);作为我想要做的一个例子,但它不起作用。我基本上希望类成为视图的接口,为了做到这一点,我需要以某种方式将XML布

我觉得我以前好像知道如何做到这一点,但我现在是一片空白。我有一个从视图(
Card
)扩展的类,我用XML为它编写了一个布局。我要做的是将
Card
的视图设置为构造函数中的XML视图,这样我就可以使用
Card
中的方法来设置
TextView
s等等。有什么建议吗?代码如下:

Card.java: (我有
View.inflate(context,R.layout.card_layout,null);
作为我想要做的一个例子,但它不起作用。我基本上希望类成为视图的接口,为了做到这一点,我需要以某种方式将XML布局分配给视图。我是否使用了类似
setContentView(视图)的东西
?在
视图
类中没有这样的方法,但是有类似的方法吗?)

card_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="336dp"
    android:layout_height="280dp"
    android:layout_gravity="center"
    android:background="@drawable/card_bg"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/tv"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:textSize="24dp"
    />

</LinearLayout>

您应该能够在布局中使用类名。比如:

<your.package.name.Card 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="336dp"
    android:layout_height="280dp"
    ...

当前设置不允许您尝试执行此操作。
视图
(或其直接子类)表示一个单个视图,它没有子视图的概念,您正在尝试做什么。
LayoutInflater
不能与简单的
视图
一起使用,因为简单的
视图
类没有实际添加子对象的方法(如
addView()
方法)

另一方面,能够生孩子的正确类是
ViewGroup
(或直接子类之一,如
LinearLayout
FrameLayout
等),它通过提供
addView
方法,接受添加
视图
或其他
视图组。最后,你的课程应该是:

public class Card extends ViewGroup {

    TextView tv;

    public Card(Context context) {
        super(context);
        View.inflate(context, R.layout.card_layout, this);
        tv = (TextView) findViewById(R.id.tv);
    }

    public Card(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        View.inflate(context, R.layout.card_layout, this);
        tv = (TextView) findViewById(R.id.tv);
    }

    public Card(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(context, R.layout.card_layout, this);
        tv = (TextView) findViewById(R.id.tv);
    }

    public void setText(String text) {
        tv.setText(text);
    }

}

如果我想起来,如果扩展
ViewGroup
,您必须覆盖
onLayout
,因此(由于您的布局文件),您应该查看扩展
LinearLayout
并用
merge
标记替换xml布局中的
LinearLayout

但是
card\u layout.xml
如何连接到
card
类?当我执行上面所写的操作时,没有显示任何内容。我查看了文档,其中提供了大量关于以编程方式绘制自定义视图的信息,但没有提供关于使用XML定义的视图的信息。我可以使用方法等绘制所有内容,但这需要一段时间,因为这是一个非常详细的布局。您需要将
卡的实例添加到另一个视图中,您可以使用
视图。addView(Card)
setContentView(Card)
进行活动。好的,我想您需要使用充气机。我将编辑我的答案来演示如何操作。我曾想过让我的类扩展
LinearLayout
,但后来认为这是一个愚蠢的想法。我在看到这个评论后试过了,效果非常好!非常感谢你!
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
Card card = (Card) inflater.inflate(R.layout.card_layout, null);
parentView.addView(card);
public class Card extends ViewGroup {

    TextView tv;

    public Card(Context context) {
        super(context);
        View.inflate(context, R.layout.card_layout, this);
        tv = (TextView) findViewById(R.id.tv);
    }

    public Card(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        View.inflate(context, R.layout.card_layout, this);
        tv = (TextView) findViewById(R.id.tv);
    }

    public Card(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(context, R.layout.card_layout, this);
        tv = (TextView) findViewById(R.id.tv);
    }

    public void setText(String text) {
        tv.setText(text);
    }

}