2016-05-11 20 views
0

Ich habe ein GridLayout und darin sind 6 LinearLayout Kinder. Darin sind Knöpfe und andere Dinge.GridLayout nicht gleichmäßig Abstand Kinder im gesamten Bildschirm

In Android Studio sieht es gut aus, aber auf einem tatsächlichen Gerät (und sogar Emulator) es nicht gleichmäßig Platz. Hier ist ein Bild von dem, was passiert. enter image description here

<RelativeLayout <!--main layout stuff--> 
> 

<GridLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:rowCount="4" 
    android:columnCount="2"> 
     <!-- 
      there are six of these inside the grid layout 
     --> 
     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_columnWeight="1" 
      android:layout_rowWeight="1"> 

      <ImageButton 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:id="@+id/button" 
       android:src="@drawable/button" 
       android:layout_gravity="center"/> 
     </LinearLayout> 

    </GridLayout> 

</RelativeLayout> 
+0

in welchem ​​Gerät testen Sie? –

+0

HTC One Mini, Samsung und Emulator –

+0

Ich empfehle Ihnen, Gridview benutzerdefinierte –

Antwort

3

Das Problem dabei ist, dass android:layout_columnWeight und android:layout_rowWeight neu in Lollipop sind und daher nicht zuvor unterstützt werden. Sie können GridLayout von support library und app:layout_columnWeight/app:layout_rowWeight verwenden, um diese Funktionen für ältere Android-Geräte zu erhalten.

<android.support.v7.widget.GridLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:rowCount="4" 
    app:columnCount="2"> 
     <!-- 
      there are six of these inside the grid layout 
     --> 
     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      app:layout_columnWeight="1" 
      app:layout_rowWeight="1"> 

      <ImageButton 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:id="@+id/button" 
       android:src="@drawable/button" 
       android:layout_gravity="center"/> 
     </LinearLayout> 

</GridLayout> 
0

Sie müssen die Höhe und Breite der LinearLayout zu 0dp so einzustellen, dass das übergeordnete Layout Kasse, um bis dieses

<RelativeLayout <!--main layout stuff--> 
> 
    <GridLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:rowCount="4" 
    android:columnCount="2"> 
     <!-- 
      there are six of these inside the grid layout 
     --> 
     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_columnWeight="1" 
      android:layout_rowWeight="1"> 

      <ImageButton 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/button" 
       android:src="@drawable/button" 
       android:layout_gravity="center"/> 
     </LinearLayout> 

    </GridLayout> 

</RelativeLayout> 
+0

zu verwenden Wenn ich versuche, dass es perfekt aussieht in Android Studio, aber wenn ich die App ausführen, sehe ich nur weiß jetzt. –

0
<ImageButton 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/button" 
     android:src="@drawable/drawer_bg" 
     android:layout_gravity="center" 
     android:scaleType="fitXY"/> 

versuchen scaleType-fitXY in Ihrem ImageButton

+0

Nein. Jetzt zeigt es nur das relative Layout an, in dem die Grid-Kinder nicht zu sehen sind, wenn ich die App starte –