动态添加视图时,我想做类似 XML attr 的操作:
android:layout_alignRight="@+id/myView1">
我的活动代码:
button1.setOnClickListener(new View.OnClickListener(){
@Override public void onClick(View v) {
RelativeLayout ll = (RelativeLayout)findViewById(R.id.aincrad);
TextView view = new TextView(Activity.this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_RIGHT,R.id.Btn1Holder);
view.setText("testing...");
view.setLayoutParams(layoutParams);
ll.addView(view, layoutParams);
}});
结果应该对齐新的文本视图保持在 Btn1Holder 的右侧