【发布时间】:2016-07-05 20:30:45
【问题描述】:
我是 Android 新手。我想水平放置 4 个按钮,左右边距相等,如下线框图所示:
我也在 Google 和 Stackoverflow 上进行了很多搜索。我试图设置 android:layout_weight="1" 。但它只从左侧设置相等的边距。我想在两侧和多个屏幕布局上设置它。我想知道应该为此应用哪些布局和属性。我在 Android Studio 中使用,主要使用 Drag-Drop 方法进行设计。
目前我的 XML 布局如下:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/frameLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:id="@+id/relativeLayout">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/b3"
android:layout_gravity="left|center_vertical"
android:onClick="buttonThree"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="true"
android:layout_alignParentRight="false"
android:layout_weight="1"
android:width="0dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="@+id/b5"
android:layout_gravity="left|center_vertical"
android:onClick="buttonFive"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/b3"
android:layout_toEndOf="@+id/b3"
android:layout_alignParentRight="false"
android:layout_alignParentLeft="false"
android:layout_weight="1"
android:width="0dp"
android:layout_alignParentBottom="false" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="@+id/b7"
android:layout_gravity="left|center_vertical"
android:onClick="buttonSeven"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/b5"
android:layout_toEndOf="@+id/b5"
android:layout_alignParentRight="false"
android:layout_weight="1"
android:width="0dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="@+id/b9"
android:layout_gravity="left|center_vertical"
android:onClick="buttonNine"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/b7"
android:layout_toEndOf="@+id/b7"
android:layout_alignParentRight="false"
android:layout_weight="1"
android:width="0dp" />
</RelativeLayout>
【问题讨论】:
-
看看
layout_weight的概念LinearLayout -
使用
LinearLayout代替RelativeLayout,layout_weight将与LinearLayout一起使用 -
不要对他的问题投反对票,他正在努力学习
标签: android android-layout button