2014-12-23 24 views
0

textView.setText在複合視圖中無法正常工作。 既指打招呼和插孔都出現... 和插孔下方顯示的佈局沒有出現,如果我把一些背景色,... 可能有人幫助...感謝和問候......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"); 

    } 
+1

'''zoneName.setText(「jack」);''out of method,you put that在「}」之後移動該方法。 –

回答

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

不工作意味着zoneName.setText(「jack」);是設置文本,但它的相對佈局的風格覆蓋...並且文本不出現,如果背景顏色被給出 –

+0

擴展'textView'和try..check我編輯的ans太 –

+0

其實它的一個相對佈局和我在相對佈局中增加了一些視圖...這就是爲什麼我不能將其作爲textview擴展。 –