【问题标题】:Custom View children are null after inflating the view in Android在 Android 中膨胀视图后,自定义视图子项为空
【发布时间】:2014-08-19 04:57:50
【问题描述】:

我正在开发一个 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 为空,即使我希望它在 init(..) 方法中设置。谁能告诉我它没有正确设置吗?提前致谢。

【问题讨论】:

  • 请显示活动代码

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


【解决方案1】:

感谢您的回答。事实证明 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());        
    }
}

【讨论】:

  • 这正是适合我的。需要在 Google 搜索结果中排名靠前。
【解决方案2】:

而不是使用根元素

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、它的宽度/高度等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    相关资源
    最近更新 更多