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

时间:2014-03-13 09:25:41

标签: java android eclipse textview

我创建了一个示例项目并在Eclipse中运行“Hello Android Application”。

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

默认情况下,我的示例项目中有一个Textview说“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()中添加此文本视图。

这是我的activity_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>

一步一步的解决方案对我有帮助,任何好的教程链接都会很明显。提前谢谢!

10 个答案:

答案 0 :(得分:9)

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

TextView dynamicTextView = new TextView(this);
dynamicTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
dynamicTextView.setText(" Hello World ");

将此textview添加到主布局

mainlayout.addView(dynamicTextView);

答案 1 :(得分:2)

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

.java文件

    @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);
    }

答案 2 :(得分:0)

将textView添加到linearlayout中。 linearLayout.addView(TextView的)。

在为linearlayout创建实例之前。

答案 3 :(得分:0)

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

如果您真的想通过代码创建View,那么您需要引用父布局。因此,不必直接将内容视图设置为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 。例如:

{{1}}

答案 4 :(得分:0)

代码:

TextView textView = new TextView(this);
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
textView.setText("Test");

mainlayout.addView(textView );

答案 5 :(得分:0)

试试这个

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;
    }

}

答案 6 :(得分:0)

假设您在.xml文件中有一个根布局,其ID为#34; my_root&#34;

LinearLayout my_root = (LinearLayout) findViewById(R.id.my_root);

创建新布局:

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL); 

创建TextView:

TextView textView = new TextView(this);

设置一些文字:

textView.setText("some text");

将TextView添加到布局:

layout.addView(textView);

最后将布局添加到根布局:

my_root.addView(layout);

答案 7 :(得分:0)

复制并粘贴此代码,希望它能为您提供帮助。

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;
}

 }

答案 8 :(得分:0)

设计

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

Java

TextView tv = findViewById(R.id.textview);
tv.setText("Kirubha");

答案 9 :(得分:-1)

如果你的activity_main xml有一个顶部的LinearLayout,其id为mylayout。

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