Lop*_*ano 46 java android textview android-relativelayout
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);
}
Run Code Online (Sandbox Code Playgroud)
我需要做那样的事情......所以它会显示为
one
two
asdfasdfsomething
Run Code Online (Sandbox Code Playgroud)
屏幕上..
WeN*_*igh 70
如果使用RelativeLayout并不重要,可以使用LinearLayout,并执行以下操作:
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
Run Code Online (Sandbox Code Playgroud)
这样做可以避免你尝试过的addRule方法.您只需使用addView()添加新的TextView.
完整代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
Bal*_*aji 20
试试这段代码:
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]);
}
Run Code Online (Sandbox Code Playgroud)
WeN*_*igh 17
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;
}
});
}
Run Code Online (Sandbox Code Playgroud)
可以修改此值以在不同的TextView中显示String数组的每个元素.
您没有为文本视图分配任何 id,但您将其作为参数tv.getId()传递给方法。addRule尝试通过设置一个唯一的ID tv.setId(int)。
您还可以使用垂直方向的 LinearLayout,这实际上可能更容易。如果没有必要的话,我更喜欢 LinearLayout 而不是relativelayout。
| 归档时间: |
|
| 查看次数: |
165071 次 |
| 最近记录: |