如何添加自定义视图内的另一个自定义视图? [英] How to add custom view inside another custom view?

查看:151
本文介绍了如何添加自定义视图内的另一个自定义视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加CustomViewBeta(扩展RelativeLayout的),以CustomViewAlpha(扩展的LinearLayout) - 的想法是,在CustomViewAlpha将举行一堆CustomViewBetas一拉一个ListView的。不管我怎么努力,这是行不通的。我不是什么也看不见 - 没有CustomViewBetas,也给了我一个NPE,当我尝试的setText在CustomViewBeta 里面的TextViews之一

CustomViewAlpha工作正常,因为它是硬codeD中的片段的XML:

 < XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout
   ...>
    < com.path.CustomViewAlpha
        机器人:ID =@ + ID / customviewalpha
        机器人:方向=垂直
        机器人:layout_gravity =center_vertical
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/>
< /的FrameLayout>
 

随着code是:

 公共类CustomViewAlpha扩展的LinearLayout {
私人语境mContext;

公共CustomViewAlpha(上下文的背景下){
    超(上下文);
    this.mContext =背景;
}

公共CustomViewAlpha(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
    this.mContext =背景;
}

公共CustomViewAlpha(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);
    this.mContext =背景;
}

公共无效addCustomViewBeta(字符串someString){
    CustomViewBeta customViewBeta =新CustomViewBeta(mContext);
    addView(customViewBeta);
}
 

CustomViewBeta不硬codeD中的片段的XML和获得的编程方式添加:

公共类CustomViewBeta扩展RelativeLayout的{     民营背景mContext;

 私人的TextView TextView的;

公共CustomViewBeta(上下文的背景下){
    超(上下文);
    this.mContext =背景;
    在里面();
}

公共CustomViewBeta(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
    this.mContext =背景;
    在里面();
}

公共CustomViewBeta(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);
    this.mContext =背景;
    在里面();
}


私人无效的init(){
    LayoutInflater.from(mContext).inflate(R.layout.customviewbeta,空,假);
    TextView的=(的TextView)findViewById(R.id.tripleexpandablelistview_category_name);
    textView.setText(ASDASDASDAD);
}
 

通过这个XML之中:

 < XML版本=1.0编码=UTF-8&GT?;
< com.path.CustomViewBeta
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT>

    <的TextView
        机器人:ID =@ + ID / customviewbeta_textview
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>

< /com.path.CustomViewBeta>
 

我通常会用在textView.setText(ASDASDASDAD)一NPE击;线,因为TextView的为空。有我丢失的东西?如果我不尝试充气的CustomViewBeta一个XML,只是做编程(加入textviews逐个编程?)?谢谢你。

解决方案

你的初始化是错误的。

 私人无效的init(){
    LayoutInflater.from(mContext).inflate(R.layout.customviewbeta,空,假);
    TextView的=(的TextView)findViewById(R.id.tripleexpandablelistview_category_name);
    textView.setText(ASDASDASDAD);
}
 

最后一个参数必须添加的TextView 的RelativeLayout.Also 的ViewGroup ,有一个静态充气的方法,这样就可以避免 LayoutInflater.from(mContext),你可以叫膨胀(mContext,R.layout.customviewbeta,这一点);

I'm trying to add CustomViewBeta (an extended RelativeLayout) to CustomViewAlpha (an extended LinearLayout) -- the idea being that the CustomViewAlpha will hold a bunch of CustomViewBetas a la a ListView. No matter what I try, it doesn't work. I either see nothing -- no CustomViewBetas, or it gives me an NPE when I try setText on one of the TextViews inside the CustomViewBeta

CustomViewAlpha works fine since it's hard-coded in the Fragment's 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>

With the code being:

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 is not hard-coded in the Fragment's XML and get's added programmatically:

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");
}

With this XML being:

<?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>

I usually get hit with a NPE on the "textView.setText("ASDASDASDAD");" line because the TextView is null. Is there something I'm missing? Should I not try to inflate an XML for the CustomViewBeta and just do it programmatically (adding the textviews one by one programmatically?)? Thanks.

解决方案

your init is wrong

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

the last parameter has to be true to add the TextViewto the RelativeLayout.Also ViewGroup, has a static inflate method, so you can avoid LayoutInflater.from(mContext), you can just call inflate(mContext, R.layout.customviewbeta, this);

这篇关于如何添加自定义视图内的另一个自定义视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆