How would one center align several buttons in a relative layout?
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=开发者_如何学C"@+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>
Wrap the buttons in a container element (a <div>
?) and center that instead.
精彩评论