【问题标题】:How to format buttons in RelativeLayout to be aligned to the center如何将RelativeLayout中的按钮格式化为与中心对齐
【发布时间】:2013-10-26 10:56:59
【问题描述】:

我希望有两个按钮,它们从屏幕的任一侧(边缘很小)到应用程序的中心。

我希望

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>

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

【问题讨论】:

    标签: android xml eclipse button android-relativelayout


    【解决方案1】:

    假设您希望两个Buttons 占据屏幕的 50%,您可以将它们放在一个 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>
    

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

    编辑

    如果您想在两个Buttons 之间留出空间,那么您可以在Buttons 之间创建一个空的View,并给它一个layout_weight,可能为0.5,然后每个Button 为1 s。如果您想在 Buttons 的每一侧留出空间,您可以做同样的事情。

    【讨论】:

    • 似乎更改为 LinearLayout 是最好的方法...该应用程序是完整的减去按钮大小,所以我只是在寻找不需要重新排列整体的修复程序布局。从相对变为线性容易吗?
    • 如果您需要放置位置,您仍然可以使用RelativeLayout 作为您的根ViewGroup,但将其余部分包装在LinearLayouts 中。取决于什么最适合你。还有其他方法,但如果我得到你想要的,我认为这是最有效的。
    • 这就是我想要的,但按钮在中心几乎彼此相接。这是我现在拥有的imgur.com/30AHZDi 没关系,我明白你在说什么,我会尝试的。非常感谢!
    • 我已编辑。看看这是否适合你。您可能需要使用weights 才能准确获得您想要的。
    猜你喜欢
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多