动态添加的视图在relativelayout中重叠

时间:2014-09-04 13:52:44

标签: android dynamic view relativelayout

我正在开发一个应用程序,其中我有button,点击button我正在动态创建button 。但buttons 重叠。我想要的是避免overlapping dynamically创建views

在我的情况下,必须使用RelativeLayout,因为我必须为这些动态创建的视图添加拖放功能。所以没有LinearLayout本身。

activity_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>

MainActivity类

    public class MainActivity extends FragmentActivity {
    Button btnAddButton;
    RelativeLayout rl1;
    int i = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnAddButton = (Button) findViewById(R.id.btnAdd);
        final RelativeLayout.LayoutParams layoutParam = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rl1 = (RelativeLayout) findViewById(R.id.relative_layout);

        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);


            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

2 个答案:

答案 0 :(得分:2)

您应该为相对布局的参数添加规则:

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);  
}

截图:

enter image description here

修改

即使您不使用循环,并添加按钮单击,它也应该起作用:

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++;
} 

希望这有帮助。

答案 1 :(得分:0)

有多种方法可以解决这个问题。

隐藏按钮并点击

显示

无需动态添加按钮。只需添加两个按钮一个是可见的,另一个是不可见的。点击 btnAddButton ,只需显示它..

      otherBtn.setVisibility(View.GONE);
      otherBtn.setVisibility(View.VISIBLE);// you can also set it in xml by visiblity="true"

使用LinearLayout

设置LinearLayout instead of RelativeLayout它会在设置方向后自动将其添加到右侧,如果没有设置,则按照@Dhruti的建议添加规则