我很喜欢使用动态布局来评论我的项目的一部分,但是当我将textview设置为动态,输出只出现在屏幕的顶部.并将输出放在其他输出上
RelativeLayout ll=(RelativeLayout) findViewById(R.id.rl); for(int i = 0; i < 20; i++) { TextView cb = new TextView(this); cb.setText("YORUMLAR"+yorum[0]+i); cb.setTextSize(30); ll.addView(cb); }
解决方法
您应该使用LinearLayout自动添加一个TextView.
假设您不能没有RelativeLayout生活,您将需要为您创建的所有TextView动态生成ids,以便将一个视图放在另一个视图下.这里是例子:
public class HelloWorld extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout); Random rnd = new Random(); int prevTextViewId = 0; for(int i = 0; i < 10; i++) { final TextView textView = new TextView(this); textView.setText("Text "+i); textView.setTextColor(rnd.nextInt() | 0xff000000); int curTextViewId = prevTextViewId + 1; textView.setId(curTextViewId); final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.BELOW,prevTextViewId); textView.setLayoutParams(params); prevTextViewId = curTextViewId; layout.addView(textView,params); } } }