textView.setText在复合视图中不起作用

时间:2014-12-23 08:21:13

标签: android java-api

textView.setText在复合视图中无法正常工作。 意味着你好和杰克都出现了...... 如果我放一些背景颜色,杰克显示在布局下面不显示,... 有人可以帮忙......谢谢和问候......

这是我的代码......

Layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
             android:id="@+id/chatLayout"
             android:layout_width="300dp"
             android:layout_height="400dp"
             >

             <TextView 
                 android:id="@+id/chatNamed"
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content"
                 android:text="Hello"
                 android:textColor="#ffffff"
                 android:textSize="20dp"
                 android:textStyle="bold"
                 android:layout_marginTop="10dp"
                 android:layout_marginLeft="10dp"
                 />


         </RelativeLayout>

CompoundView.java

public class ChatCompoundView extends RelativeLayout {

    private static TextView zoneName;



    public ChatCompoundView(Context context) {
        this(context, null);
        setView();

    }
    public ChatCompoundView(Context context, AttributeSet attrs) {
         this(context, attrs, 0);
         setView();

    }
    public ChatCompoundView(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
           setView();
    }


    public void setView()
    {
        inflate(getContext(), R.layout.chat_layout, this);
        zoneName = (TextView) findViewById(R.id.chatNamed);
    }
        zoneName.setText("jack");

    }

2 个答案:

答案 0 :(得分:0)

试试这个..

LayoutInflater mInflater;
public ChatCompoundView (Context context) {
    super(context);
     mInflater = LayoutInflater.from(context);
     setView();

}

<强>的setView():

public void setView(){
       mInflater.inflate(R.layout.custom_view, this, true);
       TextView zoneName = (TextView) findViewById(R.id.chatNamed);
       zoneName .setText("jack");
}

答案 1 :(得分:0)

是的,现在我得到了答案......实际上我的构造函数被调用了这个(上下文),...我需要调用super(context)...

Code Changed to
public ChatCompoundView(Context context) {
        super(context);
        setView();

    }
    public ChatCompoundView(Context context, AttributeSet attrs) {
         super(context, attrs);
         setView();

    }
    public ChatCompoundView(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
           setView();
    }