错误的重力应用于LinearLayout项目

时间:2017-09-29 19:37:36

标签: android xml android-layout android-gridview android-gravity

将Gravity属性应用于我的XML项目之后,它们似乎并未定位在正确的位置。如何解决这些问题以满足以下条件?:

  1. '向上移动'按钮需要位于屏幕顶部
  2. '向下移动'按钮需要位于屏幕的底部
  3. GridView需要位于屏幕的垂直中心
  4. 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>
    

    更新(akshay_shahane的建议)

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

3 个答案:

答案 0 :(得分:0)

autoCommit的权重设置为1,将GridLayout的权重设置为0dp

layout_height

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

<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居中。

答案 1 :(得分: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>

答案 2 :(得分: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>