而点击的ListView动态TextView的错误错误、动态、ListView、TextView

2023-09-07 10:35:56 作者:冷月葬花魂

我想有一个基于XML文件动态的TextView .....请帮我和我的code

I'm trying to have a dynamic textview based on the xml file..... Please help me with my code

code

MessageViewPage.java

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_view_page.xml

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

我有这个code错误

I am having an error in this code

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

请任何一个告诉我,我的问题的解决方案。

Please any one tell me the solution for my problem.

我的目标输出演示像

推荐答案

View类不具有addView method.Add动态的TextView为:

The View class doesn't have an addView method.Add dynamic TextView as:

先给在XML布局ID:

first give layout id in 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:id="@+id/layout"
android:orientation="vertical"
android:padding="5dp" 
android:background="@drawable/view">

在code:

final TextView tv1 = new TextView(this);
 LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
 LinearLayout.LayoutParams.FILL_PARENT,
  LinearLayout.LayoutParams.WRAP_CONTENT
   );
   tv1.setText("TextView");
 RelativeLayout layout = (RelativeLayout ) findViewById(R.id.layout);
 layout.addView(tv1, p);