两个其他按钮之间的中心按钮
Center button between two other buttons
我正在尝试将一个按钮置于其他两个按钮中间。我尝试使用 weight
但没有成功(Android Studio
一直说有错误),使用 layout_marginRight
不是一个好主意,因为它与分辨率无关。
这是我的 XML
代码,希望它能帮助您理解我的问题。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main"
tools:context=".MainActivity">
<TextView
android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_bottom"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="?attr/colorPrimary"
android:layout_alignParentBottom="true"
android:minHeight="?attr/actionBarSize" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center">
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/bottom_LeftButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lft"
android:layout_alignParentLeft="true" />
<Button
android:id="@+id/bottom_LeftCenterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LftC"
android:layout_alignParentLeft="true" />
<Button
android:id="@+id/bottom_CenterButtom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Cnt" />
<Button
android:id="@+id/bottom_RightCenterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RhtC"
android:layout_alignParentRight="true" />
<Button
android:id="@+id/bottom_RightButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rht"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.Toolbar>
使用线性布局然后将 "WEIGHT" 包含到您的按钮中,因为权重属于线性布局。
<LinearLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/bottom_LeftButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lft"
android:layout_weight="1"
android:layout_alignParentLeft="true" />
<Button
android:id="@+id/bottom_LeftCenterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LftC"
android:layout_weight="1"
android:layout_alignParentLeft="true" />
<Button
android:id="@+id/bottom_CenterButtom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Cnt" />
<Button
android:id="@+id/bottom_RightCenterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RhtC"
android:layout_weight="1"
android:layout_alignParentRight="true" />
<Button
android:id="@+id/bottom_RightButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rht"
android:layout_weight="1"
android:layout_alignParentRight="true" />
</LinearLayout>
要使所有按钮保持相同大小,您需要将宽度属性保持为 "fill_parent"。下面是工作 xml。 weightsum 应等于您要使用的按钮数。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
我正在尝试将一个按钮置于其他两个按钮中间。我尝试使用 weight
但没有成功(Android Studio
一直说有错误),使用 layout_marginRight
不是一个好主意,因为它与分辨率无关。
这是我的 XML
代码,希望它能帮助您理解我的问题。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main"
tools:context=".MainActivity">
<TextView
android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_bottom"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="?attr/colorPrimary"
android:layout_alignParentBottom="true"
android:minHeight="?attr/actionBarSize" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center">
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/bottom_LeftButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lft"
android:layout_alignParentLeft="true" />
<Button
android:id="@+id/bottom_LeftCenterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LftC"
android:layout_alignParentLeft="true" />
<Button
android:id="@+id/bottom_CenterButtom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Cnt" />
<Button
android:id="@+id/bottom_RightCenterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RhtC"
android:layout_alignParentRight="true" />
<Button
android:id="@+id/bottom_RightButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rht"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.Toolbar>
使用线性布局然后将 "WEIGHT" 包含到您的按钮中,因为权重属于线性布局。
<LinearLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/bottom_LeftButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lft"
android:layout_weight="1"
android:layout_alignParentLeft="true" />
<Button
android:id="@+id/bottom_LeftCenterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LftC"
android:layout_weight="1"
android:layout_alignParentLeft="true" />
<Button
android:id="@+id/bottom_CenterButtom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Cnt" />
<Button
android:id="@+id/bottom_RightCenterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RhtC"
android:layout_weight="1"
android:layout_alignParentRight="true" />
<Button
android:id="@+id/bottom_RightButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rht"
android:layout_weight="1"
android:layout_alignParentRight="true" />
</LinearLayout>
要使所有按钮保持相同大小,您需要将宽度属性保持为 "fill_parent"。下面是工作 xml。 weightsum 应等于您要使用的按钮数。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4">
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>