Android:以编程方式创建TextView

时间:2011-11-02 07:28:39

标签: android android-layout

如何使用TextView创建layout_width=fill_parent(使用Java代码,而不是xml)?

TextView应该是可编辑的。

4 个答案:

答案 0 :(得分:6)

如果我不理解错误,您需要以编程方式创建EditText。然后,尝试使用:

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

编辑:

导入LayoutParams:

import android.view.ViewGroup.LayoutParams;

答案 1 :(得分:4)

它不是TextBox,而是Android中的EditText

无论如何,您可以使用以下命令创建运行时:

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

答案 2 :(得分:0)

EditText et = new EditText(context); //这会为您提供一个新的文本框

//假设当你击中时你正处于日蚀状态。之后你会看到你可以随意设置的所有各种属性。

然后当你准备就绪时,你要么将它设置为setContentView(et); //这里它是屏幕上唯一的东西

将其添加到您已设置的任何布局中。

答案 3 :(得分:0)

通过

创建具有适当宽度和高度的layoutParams
  

LayoutParams params = //选择合适的构造函数

//注意正确导入。

现在创建textBox和setLayoutParams

  

EditText t = new EditText(this);

     

t.setLayoutParams(PARAMS);

相关问题