2013-10-17 142 views
0

我希望有两个按钮可以跨越屏幕两侧(边距较小)到应用程序的中心。如何将RelativeLayout中的按钮设置为与中心对齐

我曾希望

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> 

我将包括图像,但我没有足够的代表...

回答

0

假设你想同时Button s至占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> 

如果这个东西是不是你想要的,那么请澄清一下。

编辑

如果你想在这两个Button S之间的空间,那么你可以创建Button S之间的空View,并给它的,也许0.5则1 layout_weight每个Button S的。如果你想在Button的每一边都有空间,你可以做同样的事情。

+0

似乎更改为LinearLayout是继续此操作的最佳方式...应用程序完全减去按钮大小,因此我只是在寻找不需要重新排列整个布局的修复程序。从相对线性转换很容易吗? – Mac

+0

如果您需要进行放置,您可以使用'RelativeLayout'作为您的根'ViewGroup',但将剩下的放在'LinearLayout'中。取决于什么最适合你。还有其他的方法,但我认为这是最有效的,如果我得到你想要的。 – codeMagic

+0

这就是我想要的,但是在中心几乎相互碰撞的按钮。这里是我现在http://imgur.com/30AHZDi 没关系,我明白你在说什么我会尝试。非常感谢! – Mac