[英]Create a new TextView programmatically then display it below another TextView
String[] textArray={"one","two","asdasasdf asdf dsdaa"};
int length=textArray.length;
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for(int i=0;i<length;i++){
TextView tv=new TextView(getApplicationContext());
tv.setText(textArray[i]);
relativeParams.addRule(RelativeLayout.BELOW, tv.getId());
layout.addView(tv, relativeParams);
}
I need to do something like that.. so it would display as我需要做这样的事情..所以它会显示为
one
two
asdfasdfsomething
on the screen..屏幕上..
If it's not important to use a RelativeLayout, you could use a LinearLayout, and do this:如果使用 RelativeLayout 并不重要,则可以使用 LinearLayout,然后执行以下操作:
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
Doing this allows you to avoid the addRule method you've tried.这样做可以避免您尝试过的 addRule 方法。 You can simply use addView() to add new TextViews.
您可以简单地使用 addView() 添加新的 TextView。
Complete code:完整代码:
String[] textArray = {"One", "Two", "Three", "Four"};
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);
for( int i = 0; i < textArray.length; i++ )
{
TextView textView = new TextView(this);
textView.setText(textArray[i]);
linearLayout.addView(textView);
}
Try this code:试试这个代码:
final String[] str = {"one","two","three","asdfgf"};
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
final TextView[] tv = new TextView[10];
for (int i=0; i<str.length; i++)
{
tv[i] = new TextView(this);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams
((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT);
params.leftMargin = 50;
params.topMargin = i*50;
tv[i].setText(str[i]);
tv[i].setTextSize((float) 20);
tv[i].setPadding(20, 50, 20, 50);
tv[i].setLayoutParams(params);
rl.addView(tv[i]);
}
public View recentView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Create a relative layout and add a button
relativeLayout = new RelativeLayout(this);
btn = new Button(this);
btn.setId((int)System.currentTimeMillis());
recentView = btn;
btn.setText("Click me");
relativeLayout.addView(btn);
setContentView(relativeLayout);
btn.setOnClickListener(new View.OnClickListener() {
@Overr ide
public void onClick(View view) {
//Create a textView, set a random ID and position it below the most recently added view
textView = new TextView(ActivityName.this);
textView.setId((int)System.currentTimeMillis());
layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW, recentView.getId());
textView.setText("Time: "+System.currentTimeMillis());
relativeLayout.addView(textView, layoutParams);
recentView = textView;
}
});
}
This can be modified to display each element of a String array in different TextViews.这可以修改为在不同的 TextViews 中显示 String 数组的每个元素。
You're not assigning any id to the text view, but you're using tv.getId()
to pass it to the addRule
method as a parameter.您没有为文本视图分配任何 id,而是使用
tv.getId()
将其作为参数传递给addRule
方法。 Try to set a unique id via tv.setId(int)
.尝试通过
tv.setId(int)
设置一个唯一的 id。
You could also use the LinearLayout with vertical orientation, that might be easier actually.您还可以使用具有垂直方向的 LinearLayout,这实际上可能更容易。 I prefer LinearLayout over RelativeLayouts if not necessary otherwise.
如果没有必要,我更喜欢 LinearLayout 而不是 RelativeLayouts。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.