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

时间:2015-03-24 20:42:32

标签: android xml android-layout

我正在尝试将CustomViewBeta(一个扩展的RelativeLayout)添加到CustomViewAlpha(一个扩展的LinearLayout) - 这个想法是CustomViewAlpha将一堆CustomViewBetas保存为ListView。无论我尝试什么,它都行不通。我没有看到任何东西 - 没有CustomViewBetas,或者当我在CustomViewBeta中的一个TextViews上尝试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中进行硬编码,并以编程方式添加:

公共类CustomViewBeta扩展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;” line,因为TextView为null。有什么我想念的吗?我是否应该尝试为CustomViewBeta扩充XML并以编程方式执行(以编程方式逐个添加文本视图?)?感谢。

1 个答案:

答案 0 :(得分:3)

你的init错了

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.Also ViewGroup,并使用static inflate方法,这样您就可以避免LayoutInflater.from(mContext) },您只需致电inflate(mContext, R.layout.customviewbeta, this);