2014-04-21 24 views
0

即時通訊的建設就像一個聊天應用程序,所以我想添加元素(用戶圖片和消息)去我當前的觀點。問題出在showMessage函數中,沒有發生,但沒有錯誤。LayoutInflater不起作用

這裏是我的代碼:

ChatActivity - > showMessage:

RelativeLayout rly = new RelativeLayout(ChatActivity.this); 
rly.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
LayoutInflater inflater = getLayoutInflater(); 
View viewToAppend = inflater.inflate(R.layout.messages,null); 
rly.addView(viewToAppend); 

消息XML:

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="40dp" 
    android:layout_height="40dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/fede" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_marginLeft="10dp" 
    android:layout_toRightOf="@+id/imageView1" 
    android:text="messagetext" /> 

</RelativeLayout> 

UPDATE

添加setContentV IEW(RLY);工作,但用「xml加載」替換所有內容,我想追加它,而不是全部替換。

+2

,你在你的代碼是指用於RelativeLayout的? –

+0

嗯,不明白你的意思與「referer」,那是所有我的代碼爲該功能 –

回答

0

您可能會嘗試像代碼。如

LayoutInflater inflater = (LayoutInflater) this 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

可能會成功工作。祝你好運!

+0

沒有工作:(,謝謝 –

+0

可能是現在的作品,因爲我編輯此代碼,請再試一次 –

0

如果您想將佈局添加到充氣器,則只需使用佈局ID作爲參數的充氣方法即可。


View v = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.info_window_adapter_layout, null); 

凡上下文的參照活動上下文

+0

謝謝,但沒有工作:( –

0

您創建一個新的RelativeLayout並添加郵件查看它,但你永遠不附加的RelativeLayout到內容視圖。在佈局XML指定主佈局的ID(如「main_layout」),然後補充一點:

RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main_layout); 
mainLayout.addView(rly); 
+0

Thaks,但是如果我添加setContentView(rly),用新XML替換所有內容,我想追加它,而不是替換。 –

+0

然後,您需要將rly添加到當前內容視圖。在主佈局XML中指定一個id,然後在代碼中通過id查找該視圖並將其附加到它。 –