如何在另一个自定义视图中添加自定义视图?

6
我正在尝试将CustomViewBeta(一个扩展的RelativeLayout)添加到CustomViewAlpha(一个扩展的LinearLayout)中,目的是让CustomViewAlpha像ListView一样容纳许多个CustomViewBeta。不管我尝试什么方法,都无法实现。我要么看不见任何CustomViewBetas,要么在我尝试在CustomViewBeta内部的TextView上setText时出现NPE错误。

CustomViewAlpha正常工作,因为它在Fragment的XML中被硬编码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
   ...>
    <com.path.CustomViewAlpha
        android:id="@+id/customviewalpha"
        android:orientation="vertical"
        android:layout_gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>

代码如下:

public class CustomViewAlpha extends LinearLayout {
private Context mContext;

public CustomViewAlpha (Context context) {
    super(context);
    this.mContext = context;
}

public CustomViewAlpha (Context context, AttributeSet attrs) {
    super(context, attrs);
    this.mContext = context;
}

public CustomViewAlpha (Context context, AttributeSet attrs, int defStyle){
    super(context, attrs,defStyle);
    this.mContext = context;
}

public void addCustomViewBeta(String someString){
    CustomViewBeta customViewBeta = new CustomViewBeta(mContext);
    addView(customViewBeta);
}

CustomViewBeta 不是在 Fragment 的 XML 中硬编码的,而是通过编程方式添加:

public class CustomViewBeta extends RelativeLayout{ private Context mContext;

private TextView textView;

public CustomViewBeta (Context context) {
    super(context);
    this.mContext = context;
    init();
}

public CustomViewBeta (Context context, AttributeSet attrs) {
    super(context, attrs);
    this.mContext = context;
    init();
}

public CustomViewBeta (Context context, AttributeSet attrs, int defStyle){
    super(context, attrs,defStyle);
    this.mContext = context;
    init();
}


private void init() {
    LayoutInflater.from(mContext).inflate(R.layout.customviewbeta, null, false);
    textView = (TextView) findViewById(R.id.tripleexpandablelistview_category_name);
    textView.setText("ASDASDASDAD");
}

以下是XML示例:

<?xml version="1.0" encoding="utf-8"?>
<com.path.CustomViewBeta
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

</com.path.CustomViewBeta>

我通常会在“textView.setText(”ASDASDASDAD“);”这一行上遇到NPE,因为TextView为空。我是否遗漏了什么?我不应该尝试为CustomViewBeta填充XML,而只是通过编程方式来完成它(逐个添加TextView)吗?谢谢。

1
LayoutInflater.from(mContext).inflate(R.layout.customviewbeta, this); 你可能想要将充气的视图实际添加到CustomViewBeta中。 - user
1个回答

3

你的初始化有误

private void init() {
    LayoutInflater.from(mContext).inflate(R.layout.customviewbeta, null, false);
    textView = (TextView) findViewById(R.id.tripleexpandablelistview_category_name);
    textView.setText("ASDASDASDAD");
}

最后一个参数必须是true,以便将TextView添加到RelativeLayout中。此外,ViewGroup有一个static inflate方法,因此您可以避免使用LayoutInflater.from(mContext),只需调用inflate(mContext, R.layout.customviewbeta, this);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接