我使用两个 buttons 创建了 mainLayout。这两个按钮是 add 和 remove。
add: 添加另外的 layout
remove : 移除其它的 layout
<Button
android:id="@+id/btnAdd"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Add View"
android:onClick="addView" />
<Button
android:id="@+id/btnRemove"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Remove View"
android:onClick="removeView" />
下面的代码实现的是当我点击 addView 按钮时添加 view:
LayoutInflater inflater= (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.other_layout,null);
mainLayout.addView(view);
这个 view 是添加在 main layout下面的。我想把 view 添加在 addView 按钮的右下边,removeView 按钮的上面,但是不在 main Layout 的底部。
如何实现?
在两个按钮之间添加一个framelayout。
然后在执行时把 view放到 framelayout中。
LayoutInflater inflater= (LayoutInflater)this.getSystemService (LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.other_layout,null);
myframeLayout.addView(view);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rl .addRule(RelativeLayout.POSITION_BELOW, R.id.btnAdd);
rl .addRule(RelativeLayout.POSITION_TO_RIGHT, R.id.btnAdd);
rl .addRule(RelativeLayout.POSITION_ABOVE, R.id.btnRemove);
mainLayout.addView(view,rl);