简体   繁体   English

Android:以编程方式创建TextView

[英]Android: Creating a TextView programmatically

How can I create a TextView (with Java code, not xml) with layout_width=fill_parent ? 如何使用layout_width=fill_parent创建TextView (使用Java代码而不是xml)?

The TextView should be editable. TextView应该是可编辑的。

If i don't understand question wrongly,you need to create an EditText programatically.then,Try using: 如果我不正确地理解问题,则需要以编程方式创建一个EditText ,尝试使用:

EditText et=new EditText(context);
et.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

EDIT : 编辑:

Import for LayoutParams: 导入LayoutParams:

import android.view.ViewGroup.LayoutParams;

Its not a TextBox, its EditText in Android. 它不是TextBox,而是Android中的EditText

Anyway, you can create it run time using: 无论如何,您可以使用以下命令创建运行时:

EditText ed = new EditText(this);    // Create a new EditText

// Setting the type of input that you want
ed.setInputType(InputType.TYPE_CLASS_TEXT);       

// setting height/width for your editText
ed.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  

EditText et = new EditText(context);// this gets you a new textbox EditText et = new EditText(context); //这将为您提供一个新的文本框

//assuming you're in eclipse when you hit the . //假设您在按时处于食相。 after et you'll see all its various properties that you can set as you like. 在et之后,您将看到可以随意设置的所有各种属性。

then when you're ready you either set it as the setContentView(et);// here it's the only thing on the screen 然后,当您准备就绪时,可以将其设置为setContentView(et); //这是屏幕上唯一的东西

or 要么

add it to whatever layout you have already set. 将其添加到您已设置的任何布局中。

Create layoutParams with appropriate width and height by 创建具有适当宽度和高度的layoutParams

LayoutParams params = //choose appropriate constructor LayoutParams params = //选择适当的构造函数

//beware about right import . //注意正确的导入。

now Create textBox and setLayoutParams 现在创建textBox和setLayoutParams

EditText t = new EditText(this); EditText t =新的EditText(this);

t.setLayoutParams(params); t.setLayoutParams(params);

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

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