【问题标题】:How to create simple Android TextView and display text on it using java code?如何使用 java 代码创建简单的 Android TextView 并在其上显示文本?
【发布时间】:2014-03-13 09:25:41
【问题描述】:

我创建了一个示例项目并在 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>

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

【问题讨论】:

标签: java android eclipse textview


【解决方案1】:

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

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

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

mainlayout.addView(dynamicTextView);

【讨论】:

  • 什么是LayoutParams?实际上有几十个类称为LayoutParams
  • 我知道,我参加聚会有点晚了。但万一,如果它对任何人有帮助,视图会使用 LayoutParams 来告诉他们的父母他们想要如何布局。如果您的视图是线性布局,则必须使用 LinearLayout.LayoutParams 类。link
【解决方案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);
    }

【讨论】:

    【解决方案3】:

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

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

    最后将你的 Layout 添加到根 Layout:

    my_root.addView(layout);
    

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        推荐使用 XML 来定义布局。仅当您必须动态创建 Views 时才创建它们。

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

        【讨论】:

          【解决方案6】:

          代码:

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

          【讨论】:

          • 什么是LayoutParams?实际上有几十个类称为LayoutParams
          • 这里,LayoutParams 类取决于你的“mainlayout”视图。如果是 LinearLayout 那么类就是 LinearLayout.LayoutParams (developer.android.com/reference/android/widget/…)
          【解决方案7】:

          试试这个

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

          【讨论】:

            【解决方案8】:

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

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

            【讨论】:

              【解决方案9】:

              设计

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

              【讨论】:

                【解决方案10】:

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

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

                【讨论】:

                • layout.addView(tx);有什么用
                • @LomE999 我猜这里的 tx 是 TextView 类的对象。
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2012-09-03
                • 1970-01-01
                • 1970-01-01
                • 2012-10-20
                • 1970-01-01
                • 1970-01-01
                • 2011-12-14
                相关资源
                最近更新 更多