如何在Android中以编程方式将这两个按钮对齐?

时间:2014-04-11 06:46:28

标签: android

我写了这段代码。但它没有给出正确的结果。请告诉我哪里出错了。而且我不想使用线性布局。

这是xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >


<RelativeLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/custom_relativeLayout1"
    android:orientation="horizontal"
    android:background="#ffffff">

    </RelativeLayout>
</LinearLayout>

String[] but = {"Hello", "Bye"};
int buttonCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


  customLayout = (RelativeLayout) findViewById(R.id.custom_relativeLayout1);

// customLayout是relativelayout的对象。

    buttonCount = but.length;
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    Button [] butArray = new Button[buttonCount];


    for (int i = 0; i < 2; i++)
    {

        butArray[i] = new Button(this);
        butArray[i].setLayoutParams(params);

        RelativeLayout.LayoutParams Btnparams = (RelativeLayout.LayoutParams) butArray[i].getLayoutParams();
        butArray[i].setText(but[i]);
        butArray[i].setId(i+1); // Setting the ids

        butArray[i].setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_launcher, 0, 0);
        butArray[i].setBackgroundColor(Color.TRANSPARENT);
        if (butArray[i].getId() != 1)
        {
            Btnparams.addRule(RelativeLayout.RIGHT_OF, butArray[i-1].getId());
            butArray[i].setLayoutParams(Btnparams);
            customLayout.addView(butArray[i]);
        }
        else
        {   
            butArray[i].setLayoutParams(Btnparams);
            customLayout.addView(butArray[i]);
        }


    }

3 个答案:

答案 0 :(得分:0)

RelativeLayout.RIGHT_OFhoryzontally还是垂直对齐? 所以对于垂直

Btnparams.addRule(RelativeLayout.BELOW, butArray[i-1].getId());
customLayout.addView(butArray[i], Btnparams);

或横向

Btnparams.addRule(RelativeLayout.RIGHT_OF, butArray[i-1].getId());
customLayout.addView(butArray[i], Btnparams);

而不是

Btnparams.addRule(RelativeLayout.RIGHT_OF, butArray[i-1].getId());
            butArray[i].setLayoutParams(Btnparams);
            customLayout.addView(butArray[i]);

答案 1 :(得分:0)

LinearLayout非常适合使用,如果你想动态添加视图并按比例缩放它们。例如,如果要添加按钮并且每行最多有3个按钮,则还可以使用另一个LinearLayout创建新行。

我'滥用'了LinearLayout行和一个用于布局模板的按钮模板。

布局文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/ll_root"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <LinearLayout android:id="@+id/ll_row1"
                   android:orientation="horizontal"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content">

        <Button
            android:id="@+id/btn1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button 1"/>

        <Button
            android:id="@+id/btn2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button 2"/>

    </LinearLayout>

</LinearLayout>

示例代码:

// Get LL and template params
LinearLayout root = (LinearLayout) findViewById(R.id.ll_root);
LinearLayout row1 = (LinearLayout) findViewById(R.id.ll_row1);
ViewGroup.LayoutParams llRowParams = row1.getLayoutParams();
ViewGroup.LayoutParams btnParams = findViewById(R.id.btn1).getLayoutParams();

// Make new button
Button newBtn = new Button(context);
newBtn.setLayoutParams(btnParams);
newBtn.setText("Button 3");

// Add button to row1
row1.addView(newBtn);

// Add new row
LinearLayout row2 = new LinearLayout(context);
row2.setLayoutParams(llRowParams);
root.addView(row2);

// TODO: add buttons to new row
// TODO: some logic to decide between adding to row or creating new row and adding button

注意:代码未经过测试,但您应该明白这一点。

答案 2 :(得分:0)

 buttonCount = but.length;
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    Button [] butArray = new Button[buttonCount];


    for (int i = 0; i < 2; i++)
    {

        butArray[i] = new Button(this);
        butArray[i].setLayoutParams(params);

        RelativeLayout.LayoutParams Btnparams = (RelativeLayout.LayoutParams) butArray[i].getLayoutParams();
        butArray[i].setText(but[i]);
        butArray[i].setId(i+1); // Setting the ids

        butArray[i].setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_launcher, 0, 0);
        butArray[i].setBackgroundColor(Color.TRANSPARENT);
        if (i != 0) {  
            Btnparams.addRule(RelativeLayout.RIGHT_OF, i-1);
        }
        customLayout.addView(butArray[i], Btnparams);



    }