【发布时间】:2015-02-15 23:47:40
【问题描述】:
我需要动态创建多个相邻的 TextView。当我运行下面的代码时,结果不是我所期望的。 t1 和 t2 相距甚远
RelativeLayout journals = (RelativeLayout) findViewById(R.id.main);
layoutUserDoes = new LinearLayout(this);
layoutUserDoes.setLayoutParams(new LayoutParams(journals.getWidth() , 100));
layoutUserDoes.setY(0);
layoutUserDoes.setBackgroundColor(Color.WHITE);
journals.addView(layoutUserDoes);
TextView t1=new TextView(this);
t1.setTextSize(20);
t1.setText("t1");
t1.setBackgroundColor(Color.GRAY);
t1.setTextColor(Color.BLACK);
t1.setWidth(50);
t1.setHeight(40);
t1.setX(20);
layoutUserDoes.addView(t1);
TextView t2=new TextView(this);
t2.setTextSize(20);
t2.setText("t2");
t2.setBackgroundColor(Color.GRAY);
t2.setTextColor(Color.BLACK);
t2.setWidth(50);
t2.setHeight(40);
t2.setX(70);
layoutUserDoes.addView(t2);
【问题讨论】: