简体   繁体   English

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

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

I have created a sample project and run 'Hello Android Application' in Eclipse. 我创建了一个示例项目并在Eclipse中运行“Hello Android Application”。

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. 默认情况下,我的示例项目中有一个Textview说“Hello world”。 I want to create a Textview using Java code and display some message on it. 我想使用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()添加此textview。

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! 先感谢您!

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 将此textview添加到主布局

mainlayout.addView(dynamicTextView);

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

add the textView to the linearlayout like this. 像这样将textView添加到linearlayout。 linearLayout.addView(textView). linearLayout.addView(TextView的)。

Before create an instance for the linearlayout. 在为linearlayout创建实例之前。

It is recommended to use XML to define layouts. 建议使用XML来定义布局。 Only create View s when you have to create them dynamically. 只有在必须动态创建View时才创建View

If you really want to create TextView s by code, then you need to have a reference to the parent layout. 如果您真的想通过代码创建TextView ,那么您需要引用父布局。 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 . 因此,不必直接将内容视图设置为XML布局,而是必须使XML布局膨胀,然后将内容视图设置为该View Example: 例:

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

COde: 码:

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

mainlayout.addView(textView );

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

}

Assuming you have one root Layout in the .xml file with id "my_root" 假设您在ID为“my_root”的.xml文件中有一个根布局

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

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

 }

Design 设计

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

Java Java的

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

If your activity_main xml has a top LinearLayout with id mylayout. 如果您的activity_main xml有一个顶部的LinearLayout,其id为mylayout。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM