开发者

Two buttons matching parent side by side in Android (xml)

I am currently toying around with android and making a little bit of a test app just to try coding one. I开发者_StackOverflow中文版'm sure there's an easy way to do this but i can't figure it out. I want two buttons to be on the bottom of the screen. But, i want one to be on the left side, and one on the right side, with space in the middle.

Thanks for the help


Like this perhaps?

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

  <Button
  android:id"@+id/left_button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:text="Left"/>

  <Button
  android:id"@+id/right_button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_alignParentRight="true"
  android:text="Right"/>

</RelativeLayout>

Two buttons matching parent side by side in Android (xml)


You need to give android:layout_alignParentLeft="true" to align left and android:layout_alignParentRight="true" to align right.

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id"@+id/left_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Left"/>

<Button
android:id"@+id/right_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Right"/>

</RelativeLayout>


You can do that using either LinearLayout, TableLayout or RelativeLayout. Try it... it's not that hard.


  1. Create a linear layout (inside your linerlayout which is created by default when your creating layout)

  2. Edit your new linear layout

    android:layout_gravity="bottom"
    android:orientation="horizontal"
    android:gravity="center"
    
  3. Add two buttons inside linearlayout (add text/size also)

  4. Optional: add both button margin_layout: 10dip

    android:layout_margin="10dip"
    

That should do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜