在Android中扩展视图后,自定义视图子项为null

时间:2014-06-28 01:19:46

标签: java android android-custom-view layout-inflater

我正在开发Android应用。我有一个自定义视图和布局如下:

<com.hello.view.card.inner.SimpleCardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/card_simple_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/simple_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</com.hello.view.card.inner.SimpleCardView>

这是Java类:

public class SimpleCardView extends LinearLayout {
    protected SimpleCard card = null;
    protected TextView textView;

    public SimpleCardView(Context context) {
        super(context);     
        init(context);
    }

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

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

    protected void init(Context context) {
        textView = (TextView)findViewById(R.id.simple_label);
    }

    public void setCard(SimpleCard card) {
        this.card = card;
        textView.setText(card.getMessage());        
    }
}

这就是我如何夸大视图(我尝试了两个跟注):

SimpleCardView view = (SimpleCardView)inflater.inflate(R.layout.card_simple, null);
//SimpleCardView view = (SimpleCardView)inflater.inflate(R.layout.card_simple, parent);
view.setCard(card);

我遇到的问题是当调用view.setCard(card)时,我看到textView为null,即使我希望它在init(..)方法中设置。谁能告诉我它没有正确设置?提前谢谢。

2 个答案:

答案 0 :(得分:13)

感谢您的回答。事实证明,不应在构造函数中调用init(context)。调用它的正确位置是onFinishInflate()。以下更改有助于解决问题:

public class SimpleCardView extends LinearLayout {
    protected SimpleCard card = null;
    protected TextView textView;

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

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

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

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        init(getContext());
    }

    protected void init(Context context) {
        textView = (TextView)findViewById(R.id.simple_label);
    }

    public void setCard(SimpleCard card) {
        this.card = card;
        textView.setText(card.getMessage());        
    }
}

答案 1 :(得分:1)

而不是使用根元素

com.hello.view.card.inner.SimpleCardView

尝试使用

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:id="@+id/simple_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</merge>

然后,在你的init方法

LayoutInflater.from(context).inflate(R.layout.card_simple, this);
setOrientation(LinearLayout.VERTICAL);
textView = (TextView)findViewById(R.id.simple_label);

在其他布局中使用视图时,您希望放置

<com.hello.view.card.inner.SimpleCardView />

以及它需要的任何属性,其id,宽度/高度等

相关问题