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

How to add custom view inside another custom view?

我正在尝试将 CustomViewBeta(一个扩展的 RelativeLayout)添加到 CustomViewAlpha(一个扩展的 LinearLayout)——我的想法是 CustomViewAlpha 将包含一堆 CustomViewBetas 和 ListView。无论我尝试什么,它都不起作用。我要么什么也没看到——没有 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 扩展了 RelativeLayout{ 私有上下文 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("ASDASDADAD 上被 NPE 击中");"行,因为 TextView 为空。有什么我想念的吗?我是否应该尝试为 CustomViewBeta 膨胀 XML 并以编程方式进行(以编程方式一个接一个地添加文本视图?)?谢谢

你的初始化错误

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.AlsoViewGroup,有一个static inflate方法,所以你可以避免LayoutInflater.from(mContext),你可以调用inflate(mContext, R.layout.customviewbeta, this);