2017-09-29 17 views
-3

Après avoir appliqué les propriétés Gravity à mes éléments XML, ils ne semblent pas être positionnés au bon endroit. Comment ces problèmes peuvent être fixés de manière à ce que les conditions suivantes sont réunies ?:Mauvaise gravité appliquée aux éléments LinearLayout

  1. Le bouton doit être en haut de l'écran « déplacer vers le haut »
  2. Le bouton « déplacer vers le bas » doit être au bas de l'écran
  3. les GridView doit être au centre vertical de l'écran

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

pourquoi vous n'utilisez pas le poids? pour remonter 0,1, la grille 0,8 et descendre 0,1 –

+0

@akshay_shahane Je l'ai fait mais la gravité de la GridView continue à jouer! Il ne va pas au centre vertical! – MacaronLover

+0

a essayé de mettre la gravité de disposition de racine à centerVertical? –

Répondre

0

Soit régler le poids du GridLayout-1 et layout_height à 0DP comme celui-ci

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

Ou envelopper la présentation de la grille dans un autre récipient comme celui-ci.

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

La deuxième option serait préférable car elle permet au GridLayout de centrer en utilisant android:layout_gravity="center".

0

Essayez cette

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

juste besoin de mettre du poids 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>