3
在下面的代码中,我创建了两个文本视图并将它们以编程方式添加到相对布局。我想将它们并排排列。如何以编程方式将Android TextView对齐到另一个TextView的右边
代码运行正常但不把新TextView
先前TextView
代替新TextView
的右侧被定位在边缘(0,0,0,0),即在屏幕的右上角:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relativeLayout= (RelativeLayout) findViewById(R.id.relative_Layout);
textView[0] = new TextView(this);//creates first textview
textView[0].setId(0);
textView[0].setText("1");
textView[0].setBackgroundResource(R.drawable.shape);//parses an image from shape.xml
relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
textView[0].setLayoutParams(relativeLayoutParams);
relativeLayout.addView(textView[0]);//creates another textview
textView[1] = new TextView(this);
textView[1].setBackgroundResource(R.drawable.shape);
RelativeLayout.LayoutParams relativeLayoutParams=
new RelativeLayout.LayoutParams((RelativeLayout.LayoutParams.WRAP_CONTENT),(RelativeLayout.LayoutParams.WRAP_CONTENT));//create params for new textview
relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, textView[0].getId());//to align the textview side by side
textView[1].setText("2");
relativeLayout.addView(textView[1], relativeLayoutParams);
感谢名单马克,它解决了这一问题,但你ķ现在为什么我们不能将id设置为0? –
javadoc说:“设置这个视图的标识符,标识符在这个视图的层次结构中不必是唯一的,标识符应该是一个正数。大概'0'等于no-id-set? –
Thanx Marc。 。 。 –