-3

After applying Gravity properties to my XML items, they don't seem to be positioned in the right place. How can these issues be fixed so that the following conditions are met?:

  1. The 'move up' button needs to be at the top of the screen
  2. The 'move down' button needs to be at the bottom of the screen
  3. The GridView needs to be in the vertical centre of the screen

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn_moveup"
        android:text="move up"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="top"
        />

    <GridView
        android:id="@+id/abslistview_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:numColumns="auto_fit"
        />

    <Button
        android:id="@+id/btn_movedown"
        android:text="move down"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="bottom"
        />
</LinearLayout>

UPDATE (akshay_shahane's suggestion)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_gravity="center_vertical"
    android:orientation="vertical"
    android:weightSum="100"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn_moveup"
        android:text="move up"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_weight="5"
        android:onClick="moveup_click"
        />

    <GridView
        android:id="@+id/abslistview_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="90"
        android:layout_gravity="center_vertical"
        android:numColumns="auto_fit"
        />

    <Button
        android:id="@+id/btn_movedown"
        android:text="move down"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_weight="5"
        android:onClick="movedown_click"
        />
</LinearLayout>

enter image description here

4

3 回答 3

0

像这样将权重设置GridLayout为 1 和layout_height0dp

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn_moveup"
        android:text="move up"
        android:layout_height="wrap_content"
        android:layout_width="match_parent" />

    <GridView
        android:id="@+id/abslistview_main"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:numColumns="auto_fit"
        android:layout_weight="1"
        />

    <Button
        android:id="@+id/btn_movedown"
        android:text="move down"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        />
</LinearLayout>

或者像这样将网格布局包装在另一个容器中。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn_moveup"
        android:text="move up"
        android:layout_height="wrap_content"
        android:layout_width="match_parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <GridView
            android:id="@+id/abslistview_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:numColumns="auto_fit" />

    </LinearLayout>

    <Button
        android:id="@+id/btn_movedown"
        android:text="move down"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        />
</LinearLayout>

第二个选项会更好,因为它允许GridLayout使用android:layout_gravity="center".

于 2017-10-04T09:44:06.127 回答
0

尝试这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:weightSum="1"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn_moveup"
        android:text="move up"
        android:layout_weight="0.1"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_gravity="top"
        />

    <GridView
        android:id="@+id/abslistview_main"
        android:layout_width="match_parent"
        android:layout_weight="0.8"
        android:layout_height="0dp"
        android:layout_gravity="center_vertical"
        android:numColumns="auto_fit"
        />

    <Button
        android:id="@+id/btn_movedown"
        android:text="move down"
        android:layout_weight="0.1"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_gravity="bottom"
        />
</LinearLayout>
于 2017-10-04T09:54:12.857 回答
0

只需要将gridview权重设置为1

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
    android:id="@+id/btn_moveup"
    android:text="move up"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_gravity="top"
    />

<GridView
    android:id="@+id/abslistview_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:layout_weight="1"
    android:layout_gravity="center_vertical"
    android:numColumns="auto_fit"
    />
<Button
    android:id="@+id/btn_movedown"
    android:text="move down"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_gravity="bottom"
    />
</LinearLayout>
于 2017-10-04T10:35:36.287 回答