开发者

Center a View in Android between bottom and top views

I need somehow to do this :

Center a View in Android between bottom and top views

I 开发者_高级运维tried playing with Relative Layout, I placed top imageview and bottom button but how do I place my gridview in the center? BUT BETWEEN top image and bottom button?


Have the top image and the bottom button use wrap_content for height, and the GridView use 0dip for height along with a weight of 1. This will cause the top and bottom elements to measure first, and the GridView will get all remaining space in the middle.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView android:id="@+id/top_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <GridView android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />
    <Button android:id="@+id/bottom_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>


adamp's answer didn't work for me. This is what I found to work perfectly:

<?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" >
        <ImageView android:id="@+id/top_image"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true" />
        <Button android:id="@+id/bottom_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" />
        <LinearLayout 
            android:id="@+id/container_layout"
            android:orientation="vertical"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:layout_above="@id/bottom_button"
            android:layout_below="@id/top_image"
            android:gravity="center" >
            <GridView android:id="@+id/grid"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
        </LinearLayout>
    </RelativeLayout>

I've used this with other LinearLayouts in place of the GridView, with android:gravity="center" set on the innermost layout as well to make all of its text views and images centered horizontally as well as vertically.


use something like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center">
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/table">
        <TableRow 
            android:weightSum="100">
            <ImageView android:id="@+id/image1" 
                android:src="@drawable/icon"
                android:visibility="visible" 
                android:gravity="center"
                android:layout_weight="50" />
        </TableRow>
        <TableRow 
            android:weightSum="100">
            <GridView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content">
            </GridView>
        </TableRow>
        <TableRow 
            android:weightSum="100">
            <Button android:id="@+id/btn1"
                android:visibility="visible" 
                android:gravity="center"
                android:layout_weight="50" />
        </TableRow>
    </TableLayout>
</LinearLayout>


You could try this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView android:id="@+id/top_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <GridView android:id="@+id/grid1"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />
    <Button android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>


Add these to your Grid View:

android:layout_above="@id/bottom_button"
android:layout_below="@id/top_image"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜