2012-07-06 26 views
-1

我想基於XML的文件,有一個動態的TextView ..... 請幫我看看我的代碼想有一個動態的TextView

CODE

MessageViewPage.java

package com.dpn.pack; 

public class MessageViewPage extends Activity { 

TextView tv1=new TextView(this); 
ScrollView sv; 
String nickname,body; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.message_view_page); 

    Bundle b = getIntent().getExtras(); 

    nickname= b.getString("nick"); 
    body=b.getString("body"); 

    LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = vi.inflate(R.layout.message_view_page, null); 

    // fill in any details dynamically here 
    TextView textView = (TextView) v.findViewById(R.id.textViewSender); 
    textView.setText("Sender : "+nickname); 

    // insert into main view 
    View insertPoint = findViewById(R.id.textViewSender); 
    insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); 

} 

} 

XML文件如下

message_vi ew_page.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:padding="5dp" 
android:background="@drawable/view"> 

<TextView 
    android:id="@+id/textViewSender" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="18dp" 
    android:layout_marginTop="35dp" 
    android:textColor="#FFFFFF" 
    android:textSize="20dp" /> 

<TextView 
    android:id="@+id/textViewBody" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textViewSender" 
    android:layout_below="@+id/textViewSender" 
    android:layout_marginLeft="20dp" 
    android:layout_marginTop="40dp" 
    android:textSize="15dp" 
    android:textColor="#F69DD1" /> 

<Button 
    android:id="@+id/buttonReply" 
    android:layout_width="100dp" 
    android:layout_height="50dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_marginLeft="45dp" 
    android:layout_marginBottom="50dp" 
    android:text="Reply" /> 

<Button 
    android:id="@+id/buttonListen" 
    android:layout_width="100dp" 
    android:layout_height="50dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_marginRight="45dp" 
    android:layout_marginBottom="50dp" 
    android:text="LIsten" /> 

</RelativeLayout> 

我在此代碼

insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); 

具有類似於

enter image description here 錯誤請任何一個可以告訴我要我的問題的解決方案。

我的目標輸出演示像

enter image description here

回答

0

你有一個重複的參數:

ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT 

嘗試:

ViewGroup.LayoutParams.FILL_PARENT 
0

我可能是錯了,因爲你展示其中錯誤是,但不要說它實際是什麼。 因此,請嘗試將v轉換爲addView()方法,以傳遞您的textView

類似這樣的:insertPoint.addView(textView, 0, ...)