在低于code我已经创建了两个文本视图,并将它们添加到编程相对布局。我想通过边对齐它们身边。
在code运行良好,但没有把新的TextView
至$的右p $ pvious 的TextView
,而不是新的的TextView
被定位在边缘(0,0,0,0)即右上角屏幕的角落:
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
RelativeLayout的=(RelativeLayout的)findViewById(R.id.relative_Layout);
TextView的[0] =新的TextView(本); //创建第一的TextView
TextView的[0] .setId(0);
TextView的[0] .setText(1);
TextView的[0] .setBackgroundResource(R.drawable.shape); //解析从shape.xml图像
relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
TextView的[0] .setLayoutParams(relativeLayoutParams);
relativeLayout.addView(TextView的[0]); //创建另一个的TextView
TextView的[1] =新的TextView(本);
TextView的[1] .setBackgroundResource(R.drawable.shape);
RelativeLayout.LayoutParams relativeLayoutParams =
新RelativeLayout.LayoutParams((RelativeLayout.LayoutParams.WRAP_CONTENT),(RelativeLayout.LayoutParams.WRAP_CONTENT));//create PARAMS新的TextView
relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF,TextView的[0] .getId()); //对齐由侧TextView的侧
TextView的[1] .setText(2);
relativeLayout.addView(TextView的[1],relativeLayoutParams);
解决方案
请尝试以下操作:
设置的TextView的id [0]设置为1而不是0(ID需要是一个正整数) 加入的TextView的relativeLayoutParams [1] RelativeLayout.ALIGN_TOP规则下面为我工作:
RelativeLayout的RelativeLayout的=(RelativeLayout的)findViewById(R.id.test);
RelativeLayout.LayoutParams relativeLayoutParams;
TextView的[]的TextView =新的TextView [2];
// 1日的TextView
TextView的[0] =新的TextView(本);
relativeLayoutParams =新RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView的[0] .setId(1); //从0改为1号
TextView的[0] .setText(1);
relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
relativeLayout.addView(TextView的[0],relativeLayoutParams);
//第二的TextView
TextView的[1] =新的TextView(本);
relativeLayoutParams =新RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView的[1] .setText(2);
relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF,
TextView的[0] .getId());
relativeLayoutParams.addRule(RelativeLayout.ALIGN_TOP,
TextView的[0] .getId()); //添加顶部对齐规则
relativeLayout.addView(TextView的[1],relativeLayoutParams);
In the below code I have created two text views and added them programmatically to a relative layout. I want to align them side by side.
The code runs fine but is not placing the new TextView
to the right of previous TextView
instead the new TextView
is positioned at margin (0,0,0,0) i.e. upper right corner of the screen:
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);
解决方案
Try the following:
Set the id of textView[0] to 1 instead of 0 (id needs to be a positive integer) Add to the relativeLayoutParams of textView[1] a rule for RelativeLayout.ALIGN_TOPThe following worked for me:
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.test);
RelativeLayout.LayoutParams relativeLayoutParams;
TextView[] textView = new TextView[2];
// 1st TextView
textView[0] = new TextView(this);
relativeLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
textView[0].setId(1); // changed id from 0 to 1
textView[0].setText("1");
relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
relativeLayout.addView(textView[0], relativeLayoutParams);
// 2nd TextView
textView[1] = new TextView(this);
relativeLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
textView[1].setText("2");
relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF,
textView[0].getId());
relativeLayoutParams.addRule(RelativeLayout.ALIGN_TOP,
textView[0].getId()); // added top alignment rule
relativeLayout.addView(textView[1], relativeLayoutParams);