Android 如何动态添加文本视图。。?

Android 如何动态添加文本视图。。?,android,Android,我在android中有这段代码,我想显示用户名和密码的值。我已经使用了setcontentview,但它只显示一个textview。有人能帮我吗 Intent intent = getIntent(); String message = intent.getStringExtra(MainActivity.MESSAGE); String message2 = intent.getStringExtra(MainActivity.MESSAGE2); Text

我在android中有这段代码,我想显示用户名和密码的值。我已经使用了setcontentview,但它只显示一个textview。有人能帮我吗

    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.MESSAGE);
    String message2 = intent.getStringExtra(MainActivity.MESSAGE2);


    TextView username = new TextView(this);
    TextView password = new TextView(this);
    username.setText(message);
    password.setText(message2);

你必须再设置两个

username.setId(5);// 5 is id
username.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT));
并将其添加到您的布局中。请注意,这是xml文件中的
LinearLayout
,您必须将文本视图添加到其中

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.info)
linearLayout.addView(username);

对密码执行相同的操作

根据需要在linearlayout或relativelayout中创建xml文本视图,然后 设置文本

例如:如果您的textview 1的id在xml中定义为

android:id=“@+id/textview1”

那么在你的代码里呢-

TextView mytextview1= (TextView) view.findViewById(R.id.textview1);
同样地

如果textview 2的id在xml中定义为

android:id=“@+id/textview2”

然后在代码中执行以下操作:

TextView mytextview2= (TextView) view.findViewById(R.id.textview2);
然后简单地做:

mytextview1.setText(message);
mytextview2.setText(message2);
尝试如下:

 LinearLayout m_vwJokeLayout=(LinearLayout) this.findViewById(R.id.m_vwJokeLayout); 
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
 TextView tvUsername=new TextView(this);
 tvUsername.setLayoutParams(lparams);
 tvUsername.setText("UserName");
 this.m_vwJokeLayout.addView(tvUsername);  

 TextView tvPassword=new TextView(this);
 tvPassword.setLayoutParams(lparams);
 tvPassword.setText("Password");
  this.m_vwJokeLayout.addView(tvPassword);

尝试将两个文本视图动态添加到布局中…下面是代码

//主文件的代码

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout container=(LinearLayout)findViewById(R.id.container);

        //adding the first textview
        TextView tv1=new TextView(this);
        tv1.setText("TextView1");

        TextView tv2=new TextView(this);
        tv2.setText("TextView2");

        container.addView(tv1);
        container.addView(tv2);

    }



}
//布局文件

<LinearLayout 
    android:id="@+id/container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:orientation="vertical">


</LinearLayout>


希望它能帮助您创建线性布局或相对布局,将文本视图作为子视图添加到该布局中。然后设置ContentView(linearlayout或relativelayout);