如何将一个中心对齐多个按钮的相对布局? [英] How would one center align several buttons in a relative layout?

查看:225
本文介绍了如何将一个中心对齐多个按钮的相对布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何中心对齐的三个按钮。 web视图正确定位下面的按钮。我一直在寻找的东西是一个div在那里我可以添加layout_centerInParent属性是一样的。我中心对齐一个按钮,但没有几个,并一直没能找到合适的资源。

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android的android:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT>
    <按钮的android:layout_centerInParent =真正的机器人:的onClick =backclick机器人:layout_alignParentTop =真正的机器人:ID =@ + ID / btnBack机器人:layout_height =WRAP_CONTENT的android:layout_width =WRAP_CONTENT机器人:文本=返回机器人:layout_alignParentLeft =真正的>< /按钮>
    < ImageButton的机器人:layout_centerInParent =真正的机器人:的onClick =O单击机器人:layout_alignParentTop =真正的机器人:SRC =@绘制/ HTO机器人:ID =@ + ID / btnLogo的android:layout_width = 156dip机器人:layout_height =48dip机器人:layout_toRightOf =@ + ID / btnBack>< / ImageButton的>
    <按钮的android:layout_centerInParent =真正的机器人:的onClick =O单击机器人:layout_alignParentTop =真正的机器人:ID =@ + ID / btnRefresh机器人:layout_height =WRAP_CONTENT的android:layout_width =WRAP_CONTENT机器人:文本=家机器人:layout_toRightOf =@ + ID / btnLogo>< /按钮>
    <的WebView机器人:ID =@ + ID / web视图机器人:layout_alignParentBottom =真的android:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:layout_below =@ + ID / btnRefresh>< / - 网页视图GT;
< / RelativeLayout的>

编辑:
我修改Frankensteins code这一点。

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android的android:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT>
    <的LinearLayout机器人:ID =@ + ID / lytMenu机器人:layout_alignParentTop =真的android:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:方向=横向机器人:layout_centerInParent =真&GT ;
     <按钮安卓的onClick =backclick机器人:layout_alignParentTop =真正的机器人:ID =@ + ID / btnBack机器人:layout_height =WRAP_CONTENT的android:layout_width =WRAP_CONTENT机器人:文本=后退机器人:layout_alignParentLeft =真正的>< /按钮>
     < ImageButton的机器人:的onClick =O单击机器人:layout_alignParentTop =真正的机器人:SRC =@绘制/ HTO机器人:ID =@ + ID / btnLogo的android:layout_width =156dip机器人:layout_height = 48dip机器人:layout_toRightOf =@ + ID / btnBack>< / ImageButton的>
     <按钮安卓的onClick =O单击机器人:layout_alignParentTop =真正的机器人:ID =@ + ID / btnRefresh机器人:layout_height =WRAP_CONTENT的android:layout_width =WRAP_CONTENT机器人:文本=家机器人:layout_toRightOf =@ + ID / btnLogo>< /按钮>
    < / LinearLayout中>
   <的WebView机器人:ID =@ + ID / web视图机器人:layout_alignParentBottom =真的android:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:layout_below =@ + ID / lytMenu>< / - 网页视图GT;
< / RelativeLayout的>


解决方案

您想要的按钮布局后对方所以你应该使用和的LinearLayout这样做。如果您想要按钮垂直堆叠方向垂直:更改属性android。所述的LinearLayout在父居中。

 <?XML版本=1.0编码=UTF-8&GT?;
    <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android的android:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT>
    <的LinearLayout的android:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:方向=横向机器人:layout_centerInParent =真正的>
        <按钮安卓的onClick =backclick机器人:layout_alignParentTop =真正的机器人:ID =@ + ID / btnBack机器人:layout_height =WRAP_CONTENT的android:layout_width =WRAP_CONTENT机器人:文本=后退机器人:layout_alignParentLeft =真正的>< /按钮>
        < ImageButton的机器人:的onClick =O单击机器人:layout_alignParentTop =真正的机器人:SRC =@绘制/ HTO机器人:ID =@ + ID / btnLogo的android:layout_width =156dip机器人:layout_height = 48dip机器人:layout_toRightOf =@ + ID / btnBack>< / ImageButton的>
        <按钮安卓的onClick =O单击机器人:layout_alignParentTop =真正的机器人:ID =@ + ID / btnRefresh机器人:layout_height =WRAP_CONTENT的android:layout_width =WRAP_CONTENT机器人:文本=家机器人:layout_toRightOf =@ + ID / btnLogo>< /按钮>
    < / LinearLayout中>
    <的WebView机器人:ID =@ + ID / web视图机器人:layout_alignParentBottom =真的android:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:layout_below =@ + ID / btnRefresh>< / - 网页视图GT;
< / RelativeLayout的>

I am trying to figure out how center align the three buttons. The webview is correctly positioned below the buttons. I have been looking for something that is the equivalent of a div where I could add the layout_centerInParent attribute. I can center align one button but not several and haven't been able to find the right resource.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <Button android:layout_centerInParent="true" android:onClick="backclick" android:layout_alignParentTop="true" android:id="@+id/btnBack" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Back" android:layout_alignParentLeft="true"></Button>
    <ImageButton android:layout_centerInParent="true" android:onClick="oclick" android:layout_alignParentTop="true" android:src="@drawable/hto" android:id="@+id/btnLogo" android:layout_width="156dip" android:layout_height="48dip" android:layout_toRightOf="@+id/btnBack"></ImageButton>
    <Button android:layout_centerInParent="true" android:onClick="oclick" android:layout_alignParentTop="true" android:id="@+id/btnRefresh" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Home" android:layout_toRightOf="@+id/btnLogo"></Button>
    <WebView android:id="@+id/webView" android:layout_alignParentBottom="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btnRefresh"></WebView>
</RelativeLayout>

Edit: I modified Frankensteins code to this.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <LinearLayout android:id="@+id/lytMenu" android:layout_alignParentTop="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_centerInParent="true">   
     <Button android:onClick="backclick" android:layout_alignParentTop="true" android:id="@+id/btnBack" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Back" android:layout_alignParentLeft="true"></Button>
     <ImageButton android:onClick="oclick" android:layout_alignParentTop="true" android:src="@drawable/hto" android:id="@+id/btnLogo" android:layout_width="156dip" android: layout_height="48dip" android:layout_toRightOf="@+id/btnBack"></ImageButton>
     <Button android:onClick="oclick" android:layout_alignParentTop="true" android:id="@+id/btnRefresh" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Home" android:layout_toRightOf="@+id/btnLogo"></Button>
    </LinearLayout>    
   <WebView android:id="@+id/webView" android:layout_alignParentBottom="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/lytMenu"></WebView>
</RelativeLayout>

解决方案

You want the buttons to layout after each other so you should use and LinearLayout to do so. Change the attribute android:orientation to vertical if you want the buttons to stack vertically. The LinearLayout is centered in the parent.

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_centerInParent="true">
        <Button android:onClick="backclick" android:layout_alignParentTop="true" android:id="@+id/btnBack" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Back" android:layout_alignParentLeft="true"></Button>
        <ImageButton android:onClick="oclick" android:layout_alignParentTop="true" android:src="@drawable/hto" android:id="@+id/btnLogo" android:layout_width="156dip" android:layout_height="48dip" android:layout_toRightOf="@+id/btnBack"></ImageButton>
        <Button android:onClick="oclick" android:layout_alignParentTop="true" android:id="@+id/btnRefresh" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Home" android:layout_toRightOf="@+id/btnLogo"></Button>
    </LinearLayout>
    <WebView android:id="@+id/webView" android:layout_alignParentBottom="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btnRefresh"></WebView>
</RelativeLayout>

这篇关于如何将一个中心对齐多个按钮的相对布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆