在Android中以编程方式设置textview
我目前有:在Android中以编程方式设置textview,android,Android,我目前有: final TextView tv = new TextView(this); final RelativeLayout rL = new RelativeLayout(this); final EditText editText = (EditText)findViewById(R.id.editText); final Button b1 = (Button)findViewById(R.id.b1); b1.setOnClickListener(new Vi
final TextView tv = new TextView(this);
final RelativeLayout rL = new RelativeLayout(this);
final EditText editText = (EditText)findViewById(R.id.editText);
final Button b1 = (Button)findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rL.addView(tv);
tv.setText(editText.getText());
editText.setText("");
}
});
在我的onCreate方法中,但当输入文本并按下我的按钮时,我的文本视图不会显示在屏幕上?在我的手机屏幕上,是否有代码设置它的设置位置?这是您的问题
final RelativeLayout rL = new RelativeLayout(this);
这个包含TextView的RelativeLayout甚至没有显示在屏幕上,您所做的只是创建一个RelativeLayout
您应该做的是将RelativeLayout添加到XML布局文件(同一个包含EditText和Button的文件),然后执行以下操作
final RelativeLayout rL = (RelativeLayout)findViewById(R.id.myRelativeLayout);
...
rL.addView(tv);
现在,由于您正在引用实际的RelativeLayout,您的文本将可见。
希望我有点道理。这是你的问题
final RelativeLayout rL = new RelativeLayout(this);
这个包含TextView的RelativeLayout甚至没有显示在屏幕上,您所做的只是创建一个RelativeLayout
您应该做的是将RelativeLayout添加到XML布局文件(同一个包含EditText和Button的文件),然后执行以下操作
final RelativeLayout rL = (RelativeLayout)findViewById(R.id.myRelativeLayout);
...
rL.addView(tv);
现在,由于您正在引用实际的RelativeLayout,您的文本将可见。
希望我有点道理。你有基本布局吗?你正在将编辑文本添加到RelativeLayout中,但你需要将RelativeLayout添加到一些已经存在的布局中 首先,膨胀一些基本布局。然后在该布局上执行findViewById。使用此调用addView(editText)
公共类MyActivity扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
RelativeLayout rl=(RelativeLayout)findViewById(R.layout.base_布局);
rl.addView(yourTextView);
}
}
您有基本布局吗?您正在将EditText添加到RelativeLayout中,但需要将RelativeLayout添加到一些现有布局中
首先,膨胀一些基本布局。然后在该布局上执行findViewById。使用此调用addView(editText)
公共类MyActivity扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
RelativeLayout rl=(RelativeLayout)findViewById(R.layout.base_布局);
rl.addView(yourTextView);
}
}
您是以编程方式还是以XML格式创建textView
吗?也可以将final
替换为,比如说,public
。以编程方式,如我在标题中所述:)您添加textView的相对布局在屏幕上不可见。您需要使用findViewById()获取对您的RelativeLayout的引用就像您使用按钮和编辑文本而不是使用新的相对性一样,您是以编程方式还是以XML形式创建textView
呢?也可以将final
替换为,比如说,public
。以编程方式,如我在标题中所述:)您添加textView的相对布局在屏幕上不可见。您需要使用findViewById()获取对RelativeLayout的引用,就像使用button和EditText一样,而不是使用新的RelativeLayout