如何创建简单的Android TextView并使用java代码显示文本? [英] How to create simple Android TextView and display text on it using java code?

查看:227
本文介绍了如何创建简单的Android TextView并使用java代码显示文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是Android开发的自学习者。到目前为止,我已经能够创建一个示例项目,并在Eclipse中运行Hello Android Application。我已经知道Textview可以用两种方式创建。 ie.1使用xml标签和2.使用Java代码。
默认情况下,我的示例项目中有一个Textview说Hello world。我想使用Java代码创建一个文本视图并在其上显示一些消息。
我搜索了很多,但无法理解代码中提到的步骤和布局设置。

Hello everyone I am a self learner of Android development. Until now I have been able to create a sample project and run Hello Android Application in Eclipse. I have come to know that Textview can be created in two way. ie.1 using xml tag and 2. using Java code. By default I have one Textview saying Hello world in my sample project. I want to create a text view using Java code and display some message on it. I have searched a lot, but unable to understand the steps and layout setting mentioned in the code.

这是我所做的:

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()中添加此textview。

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

这是我的activity_main.xml

This is my 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

mainlayout.addView(dynamicTextView);

这篇关于如何创建简单的Android TextView并使用java代码显示文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆