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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22374117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:21:46  来源:igfitidea点击:

How to create simple Android TextView and display text on it using java code?

javaandroideclipsetextview

提问by SachiN Ware.

I have created a sample project and run 'Hello Android Application' in Eclipse.

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

I have learned that a Textview can be created in two ways, either using an XML tag or by using Java code.

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

By default I have one Textview saying "Hello world" in my sample project. I want to create a Textview using Java code and display some message on it.

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

I have searched a lot, but I am unable to understand the steps and layout settings mentioned in the code.

查了很多,还是看不懂代码中提到的步骤和布局设置。

This is what I have done:

这就是我所做的:

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

Further I don't know how to add this textview in addView().

此外,我不知道如何在addView().

This is my activity_main.xml:

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

A step by step solution would be helpful for me and any good tutorial link would be appreciable. Thank you in advance!

一步一步的解决方案对我有帮助,任何好的教程链接都是可观的。先感谢您!

采纳答案by RajaReddy PolamReddy

Use this code, Create text view and set layout params

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

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

add this textview to the main layout

将此文本视图添加到主布局

mainlayout.addView(dynamicTextView);

回答by tianwei

If your activity_main xml has a top LinearLayout with id mylayout.

如果您的 activity_main xml 有一个带有 id mylayout 的顶级 LinearLayout。

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

回答by Shriram

add the textView to the linearlayout like this. linearLayout.addView(textView).

像这样将 textView 添加到线性布局中。linearLayout.addView(textView)。

Before create an instance for the linearlayout.

在为线性布局创建实例之前。

回答by Mark Buikema

It is recommended to use XML to define layouts. Only create Views when you have to create them dynamically.

建议使用 XML 来定义布局。仅View当您必须动态创建它们时才 create 。

If you really want to create TextViews by code, then you need to have a reference to the parent layout. So instead of setting the content view to an XML layout directly, you would have to inflate the XML layout and then set the content view to that View. Example:

如果你真的想TextView通过代码创建s,那么你需要对父布局有一个引用。因此,您不必直接将内容视图设置为 XML 布局,而是必须扩充 XML 布局,然后将内容视图设置为该View. 例子:

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

回答by Yashdeep Patel

COde:

代码:

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

mainlayout.addView(textView );

回答by Jitesh Dalsaniya

try this

尝试这个

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

}

回答by ottse

Assuming you have one root Layout in the .xml file with id "my_root"

假设您在 .xml 文件中有一个根布局,其 ID 为“my_root”

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

Create a new Layout:

创建一个新的布局:

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

Create a TextView:

创建一个文本视图:

TextView textView = new TextView(this);

Set some text:

设置一些文字:

textView.setText("some text");

Add your TextView to the Layout:

将您的 TextView 添加到布局:

layout.addView(textView);

Finally add yout Layout to the root Layout:

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

my_root.addView(layout);

回答by IPL10

copy and paste this code hope it will help you.

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

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

 }

回答by Prakash Gajera

.xml file

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

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

回答by kirubha sankar

Design

设计

 <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");