android RelativeLayout, equal spacing?

前端 未结 2 431
北荒
北荒 2021-01-17 14:48

I\'m using Relative Layout to create a calculator screen, and i have a row of buttons labeled 1, 2, and 3. I want them evenly spaced, but i\'m not sure how to do this with

相关标签:
2条回答
  • 2021-01-17 15:24

    If you want equal spacing, you do not want a RelativeLayout. Use a LinearLayout, as you suggest in your question. If needed, put the Buttons in a LinearLayout and put the LinearLayout in the RelativeLayout.

    0 讨论(0)
  • 2021-01-17 15:48

    The easiest would be to manipulate the view during OnCreate().

    Something like:

    OnCreate(Context ctx){
      int w = getWidth();
      int h = getHeight();
      Button B1 = findViewById(R.id.button1);
      B1.setWidth(w/3);
      --repeat for button2,3 textview--
    }
    

    Another option would be to use a TableLayout where you stretch the columns and force the buttons to fill parent. This would require you to use

    <TableLayout    android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="0,1,2"
        android:layout_below="@+id/textView"> 
    <TableRow><Button
    android:id="@+id/button1"
    android:text="1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    /><Button
    android:id="@+id/button2"
    android:text="2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />
    </TableRow>
    </TableLayout>
    
    0 讨论(0)
提交回复
热议问题