如何使用java代码创建简单的Android TextView并在其上显示文本?

如何使用java代码创建简单的Android TextView并在其上显示文本?,java,android,eclipse,textview,Java,Android,Eclipse,Textview,我创建了一个示例项目,并在Eclipse中运行“Hello Android应用程序” 我了解到,可以通过两种方式创建Textview,一种是使用XML标记,另一种是使用Java代码 默认情况下,在我的示例项目中有一个文本视图显示“Hello world”。我想用Java代码创建一个Textview,并在上面显示一些消息 我已经搜索了很多,但我无法理解代码中提到的步骤和布局设置 这就是我所做的: import android.app.Activity; import android.view.Me

我创建了一个示例项目,并在Eclipse中运行“Hello Android应用程序”

我了解到,可以通过两种方式创建Textview,一种是使用XML标记,另一种是使用Java代码

默认情况下,在我的示例项目中有一个文本视图显示“Hello world”。我想用Java代码创建一个Textview,并在上面显示一些消息

我已经搜索了很多,但我无法理解代码中提到的步骤和布局设置

这就是我所做的:

import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.*;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout.LayoutParams params = 
                new LinearLayout.LayoutParams( 
                   ViewGroup.LayoutParams.WRAP_CONTENT, 
                   ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);

        TextView tx= new TextView(this);
//      tx.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        tx.setText("ANDROID APP");
        lay
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
此外,我不知道如何在
addView()
中添加此文本视图

这是我的
活动\u main.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >
</RelativeLayout>


一个循序渐进的解决方案将有助于我和任何良好的教程链接将是可观的。提前谢谢你

如果您的活动\u主xml具有id为mylayout的顶行布局

LinearLayout layout = (LinearLayout)findViewById(R.id.mylayout);
layout.addView(tx);

像这样将textView添加到linearlayout。 linearLayout.addView(文本视图)


在为linearlayout创建实例之前。

使用此代码,创建文本视图并设置布局参数

TextView dynamicTextView = new TextView(this);
dynamicTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
dynamicTextView.setText(" Hello World ");
将此文本视图添加到主布局

mainlayout.addView(dynamicTextView);

建议使用XML来定义布局。只有在必须动态创建视图时才能创建视图

如果确实要按代码创建
TextView
s,则需要有对父布局的引用。因此,您不必直接将内容视图设置为XML布局,而必须膨胀XML布局,然后将内容视图设置为该视图。例如:

View view = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
setContentView(view);

LinearLayout.LayoutParams params = 
                new LinearLayout.LayoutParams( 
                   ViewGroup.LayoutParams.WRAP_CONTENT, 
                   ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);

TextView tx= new TextView(this);
//      tx.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tx.setText("ANDROID APP");

view.addView(tx); //here the textview is attached to the parent
代码:

试试这个

import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.*;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout layout = (LinearLayout)findViewById(yourlayoutid from xml file);
        LinearLayout.LayoutParams params = 
                new LinearLayout.LayoutParams( 
                   ViewGroup.LayoutParams.WRAP_CONTENT, 
                   ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);

        TextView tx= new TextView(this);
//      tx.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        tx.setText("ANDROID APP");
        layout.add(tx);


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

假设在.xml文件中有一个id为“my_root”的根布局

创建新布局:

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL); 
创建文本视图:

TextView textView = new TextView(this);
设置一些文本:

textView.setText("some text");
将文本视图添加到布局中:

layout.addView(textView);
最后,将yout布局添加到根布局:

my_root.addView(layout);

复制并粘贴此代码,希望对您有所帮助

import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.widget.*;
import android.view.View;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   LinearLayout ll = new LinearLayout(this);

    TextView tx= new TextView(this);
    tx.setText("ANDROID APP");
    ll.addView(tx);

    setContentView(ll);


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

 }

.xml文件

<LinearLayout 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="com.example.demo.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String name="Prakash Gajera";
        TextView tv=(TextView)findViewById(R.id.textView1);
        tv.setText(name);
    }
设计


教程-@SRW-782请选择正确答案,以便将其标记为已回答。获取逐步过程,了解layout.addView(tx)的用途@LomE999我猜tx是TextView类的对象。什么是
LayoutParams
?实际上有几十个类叫做
LayoutParams
。我知道,我参加聚会有点晚了。但在这种情况下,如果对任何人都有帮助的话,视图会使用LayoutParams来告诉它们的父母它们希望如何布局。如果视图是线性布局,则必须使用LinearLayout.LayoutParams类。什么是
LayoutParams
?实际上有几十个类叫做
LayoutParams
。这里,LayoutParams类取决于“mainlayout”视图。如果是LinearLayout,则类将是LinearLayout.LayoutParams()
<LinearLayout 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="com.example.demo.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String name="Prakash Gajera";
        TextView tv=(TextView)findViewById(R.id.textView1);
        tv.setText(name);
    }
 <TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
TextView tv = findViewById(R.id.textview);
tv.setText("Kirubha");