Android 动态添加的视图在relativelayout中重叠
我正在开发一个应用程序,其中有一个Android 动态添加的视图在relativelayout中重叠,android,dynamic,view,android-relativelayout,Android,Dynamic,View,Android Relativelayout,我正在开发一个应用程序,其中有一个按钮,单击该按钮我就动态创建了一个按钮。但是按钮是重叠的。我只想避免动态创建的视图的重叠 在我的例子中,必须使用RelativeLayout,因为我必须向这些动态创建的视图添加拖放功能。因此,就其本身而言,没有线性布局。 活动\u main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linear_la
按钮
,单击该按钮
我就动态创建了一个按钮
。但是按钮
是重叠的。我只想避免动态创建的视图的重叠
在我的例子中,必须使用RelativeLayout,因为我必须向这些动态创建的视图添加拖放功能。因此,就其本身而言,没有线性布局。
活动\u main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="AddButton"
android:text="Button" />
<RelativeLayout
android:id="@+id/relative_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/btnAdd" >
</RelativeLayout>
</RelativeLayout>
您应该将规则添加到相关布局的参数中:
btnAddButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
i++;
Button btn = new Button(getApplicationContext());
btn.setId(i);
if(i>0){
layoutParam.addRule(RelativeLayout.RIGHT_OF, (i-1));
}
btn.setText("Button" + i);
rl1.addView(btn, layoutParam);
}
});
这样,您的按钮将被放置在最后一个按钮的右侧
希望这有帮助
编辑:
尝试将您的LayoutParams放入循环中。
这对我来说很好:
RelativeLayout rl1 = (RelativeLayout) findViewById(R.id.rl);
for (int i = 0; i < 4; i++) {
Button btn = new Button(getApplicationContext());
btn.setId(i);
RelativeLayout.LayoutParams layoutParam = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (i > 0) {
layoutParam.addRule(RelativeLayout.RIGHT_OF, (i - 1));
}
btn.setText("Button" + i);
rl1.addView(btn, layoutParam);
}
希望这能有所帮助。有多种方法可以解决这个问题
隐藏按钮并在单击时显示它
无需动态添加按钮。只需添加两个按钮,一个可见,另一个不可见。单击btnAddButton即可显示它
otherBtn.setVisibility(View.GONE);
otherBtn.setVisibility(View.VISIBLE);// you can also set it in xml by visiblity="true"
改用线性布局
采用线性布局而不是相对布局
它会在设置方向后自动将其添加到右侧,如果未设置,则按照@Dhruti的建议添加规则,因为这是一个相对布局,您需要使用LayoutParams在视图中定位新元素。只需将一个线性布局设置为水平方向。新元素将位于另一个元素的右侧,如果发生了什么事情,请添加规则。@Nepster,问题是我们需要使用相对布局,因为稍后我们要将拖动和拖放应用于这些视图LinearLayout
只允许我们垂直或水平移动,而不允许其他方式。隐藏按钮并在单击LayoutParams layoutParam时显示它;我已经将它用于您的代码,但仍然得到errorRelativeLayout.LayoutParams layoutParam=新的RelativeLayout.LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容);我也使用过它,但按钮仍然重叠代码>@Hanishharmathen的id可能有问题。请调试@Hanishharma用新代码和屏幕截图更新问题。同时添加按钮1 2 3。我的要求是点击按钮1,然后点击按钮2,然后点击按钮3,依此类推。。这样用户可以根据需要添加这么多按钮
int i = 1;
//this is a method being called on button's click:
public void add(View v) {
Button btn = new Button(getApplicationContext());
btn.setId(i);
RelativeLayout.LayoutParams layoutParam = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (i > 1) {
layoutParam.addRule(RelativeLayout.RIGHT_OF, (i - 1));
}
btn.setText("Button" + i);
rl1.addView(btn, layoutParam);
i++;
}
otherBtn.setVisibility(View.GONE);
otherBtn.setVisibility(View.VISIBLE);// you can also set it in xml by visiblity="true"