应用于LinearLayout项目的错误重力

问题描述:

将Gravity属性应用于我的XML项目后,它们看起来并没有放在正确的位置。如何能够将这些问题被固定成满足以下条件?:应用于LinearLayout项目的错误重力

  1. 的“上移”按钮,必须在屏幕
  2. 的“下移”按钮必须在顶部屏幕的底部
  3. GridView需要是在屏幕

activity_main.xml中

的垂直中心3210

UPDATE(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

+1

为什么你没有使用权?向上移动0.1,栅格0.8和向下移动0.1 –

+0

@akshay_shahane我做到了,但GridView的重力不断在弹起!它不会去垂直中心! – MacaronLover

+0

尝试将根布局重力设置为centerVertical? –

要么设置GridLayout 1和layout_height的重量0dp这样

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

试试这个

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

只需要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>