0

我想要两个按钮,它们从屏幕的两侧(边缘很小)到应用程序的中心。

我曾希望

android:layout_alignParentLeft="true" 

如果我输入会一样

android:layout_alignParentcenter="true"

但它不一样。

这是我的 XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/cath"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/cmhs"
        android:layout_alignBottom="@+id/cmhs"
        android:layout_alignLeft="@+id/mhsdir"
        android:layout_alignParentRight="true"
        android:background="@drawable/button_custom"
        android:text="@string/cath"
        android:textColor="#000000" />

    <Button
        android:id="@+id/cfeel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/sclose"
        android:layout_alignLeft="@+id/cath"
        android:layout_alignRight="@+id/cath"
        android:background="@drawable/button_custom"
        android:text="@string/cfeel"
        android:textColor="#000000" />

    <Button
        android:id="@+id/mhshome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_toRightOf="@+id/textView2"
        android:background="@drawable/button_custom"
        android:onClick="onClick"
        android:text="@string/mhsdir"
        android:layout_marginTop="15dp"
        android:textColor="#000000" />

    <Button
        android:id="@+id/mhsdir"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/mhshome"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/pp"
        android:background="@drawable/button_custom"
        android:text="@string/webclo"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/weblink"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/webclo"
        style="@style/AppBaseTheme"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/pp"
        android:layout_alignRight="@+id/cmhs"
        android:layout_below="@+id/textView1"
        android:background="@drawable/button_custom"
        android:text="@string/mhshome"
        android:layout_marginTop="15dp"
        android:textColor="#000000" />

    <Button
        android:id="@+id/pp"
        style="@style/AppBaseTheme"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/cmhs"
        android:layout_alignRight="@+id/cmhs"
        android:layout_below="@+id/webclo"
        android:layout_marginTop="20dp"
        android:background="@drawable/button_custom"
        android:text="@string/pp"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mhsdir"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="@string/phonenum"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/cmhs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/sclose"
        android:layout_alignRight="@+id/sclose"
        android:layout_below="@+id/textView2"
        android:background="@drawable/button_custom"
        android:text="@string/callmhs"
        android:textColor="#000000" />

    <Button
        android:id="@+id/sclose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/cmhs"
        android:layout_marginTop="20dp"
        android:background="@drawable/button_custom"
        android:text="@string/sclose"
        android:textColor="#000000" />

    <Button
        android:id="@+id/school"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/ath"
        android:layout_alignBottom="@+id/ath"
        android:layout_alignLeft="@+id/cfeel"
        android:layout_alignRight="@+id/cfeel"
        android:background="@drawable/button_custom"
        android:text="@string/school"
        android:textColor="#000000" />

    <Button
        android:id="@+id/ath"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/sclose"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/sclose"
        android:layout_marginBottom="10dp"
        android:background="@drawable/button_custom"
        android:text="@string/ath"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/school"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="15dp"
        android:text="@string/cal"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

我会附上一张照片,但我没有足够的代表...

4

1 回答 1

0

假设您希望两个Buttons 占据屏幕的 50%,您可以将它们放在 a 中LinearLayout并给它们每个相同的weight。就像是

<LinearLayout
    ...
>
    <Button
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"/>
    <Button
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"/>
</LinearLayout>

如果这不是您想要的,请澄清。

编辑

如果您想要两个 s 之间的空间,那么您可以在 s 之间Button创建一个空白,并为每个 s 给它一个可能为 0.5 的值,然后为 1 。如果你想在s 的每一边都有空间,你可以做同样的事情。ViewButtonlayout_weightButtonButton

于 2013-10-17T19:32:45.613 回答