Центрировать представление в Android между нижним и верхним видами - PullRequest
2 голосов
/ 15 мая 2011

Мне нужно как-то сделать это:

Ответы [ 5 ]

6 голосов
/ 15 мая 2011

Для верхнего изображения и нижней кнопки используйте wrap_content для высоты, а для GridView используйте 0dip для высоты вместе с весом 1. Это приведет к тому, что сначала будут измерены верхний и нижний элементы, а GridViewполучить все оставшееся место посередине.

<?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>
2 голосов
/ 19 марта 2012

ответ Адама не сработал для меня.Это то, что я нашел, чтобы работать отлично:

<?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>

Я использовал это с другими LinearLayouts вместо GridView, с android:gravity="center", установленным на самой внутренней компоновке, чтобы сделать всетекстовых изображений и изображений по центру как по горизонтали, так и по вертикали.

0 голосов
/ 23 июля 2014

Добавьте их в сетку:

android:layout_above="@id/bottom_button"
android:layout_below="@id/top_image"
0 голосов
/ 23 июля 2014

Вы можете попробовать это:

<?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>
0 голосов
/ 15 мая 2011

используйте что-то вроде этого

<?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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...