2017-10-10 189 views
0

所以我正在做一個簡單的遊戲應用程序,它有五顏六色的按鈕,其中的答案。一切工作正常,但我有一些GridLayout按鈕的大小問題。我試圖在其他帖子中搜索答案,但無法使按鈕的大小均勻。它在仿真器上看起來很好,但不在我的設備上(API 21)。是的,我嘗試將按鈕寬度更改爲「wrap_content」。GridLayout按鈕大小不同

真實的設備: https://i.stack.imgur.com/mtC25.jpg

模擬器: https://i.stack.imgur.com/sXTAG.png

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:visibility="visible" 
tools:context="com.example.adam.braintrainer.MainActivity"> 

<Button 
    android:id="@+id/goButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="false" 
    android:layout_alignParentStart="false" 
    android:layout_alignParentTop="false" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:background="@android:color/holo_blue_dark" 
    android:onClick="goButton" 
    android:padding="30dp" 
    android:text="START!" 
    android:textSize="36sp" 
    android:visibility="visible" 
    tools:gravity="center_vertical|center_horizontal|center" /> 

<RelativeLayout 
    android:id="@+id/allStuff" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentTop="true" 
    android:visibility="invisible"> 


    <TextView 
     android:id="@+id/answer" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="20dp" 
     android:text="Correct" 
     android:textSize="20sp" 
     android:visibility="invisible" /> 

    <Button 
     android:id="@+id/again" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 

     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="65dp" 
     android:onClick="playAgain" 
     android:padding="10dp" 
     android:text="PLAY AGAIN" 
     android:visibility="invisible" /> 

    <TextView 
     android:id="@+id/timer" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:layout_margin="15dp" 
     android:background="@android:color/holo_orange_light" 
     android:padding="15dp" 
     android:text="30s" 
     android:textSize="24sp" /> 

    <TextView 
     android:id="@+id/score" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_margin="15dp" 
     android:background="@color/colorAccent" 
     android:padding="15dp" 
     android:text="0/0" 
     android:textSize="24sp" /> 

    <TextView 
     android:id="@+id/suma" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/timer" 
     android:layout_alignBottom="@+id/timer" 
     android:layout_centerHorizontal="true" 
     android:text="10+13" 
     android:textSize="30dp" /> 

    <GridLayout 
     android:id="@+id/gridLayout" 
     android:layout_width="match_parent" 
     android:layout_height="300dp" 
     android:layout_alignBottom="@+id/again" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:columnCount="2" 
     android:rowCount="2"> 


     <Button 
      android:id="@+id/button4" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_columnWeight="1" 
      android:layout_gravity="fill" 
      android:layout_rowWeight="1" 
      android:background="@color/colorPrimary" 
      android:onClick="pressed" 
      android:tag="0" 
      android:text="Button" 
      android:textSize="20sp" /> 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_columnWeight="1" 
      android:layout_gravity="fill" 
      android:layout_rowWeight="1" 
      android:background="@android:color/holo_blue_dark" 
      android:onClick="pressed" 
      android:tag="1" 
      android:text="Button" 
      android:textSize="20sp" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_columnWeight="1" 
      android:layout_gravity="fill" 
      android:layout_rowWeight="1" 
      android:background="@android:color/holo_green_light" 
      android:onClick="pressed" 
      android:tag="2" 
      android:text="Button" 
      android:textSize="20sp" /> 

     <Button 
      android:id="@+id/button" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_columnWeight="1" 
      android:layout_gravity="fill" 
      android:layout_rowWeight="1" 
      android:background="@android:color/holo_red_light" 
      android:onClick="pressed" 
      android:tag="3" 
      android:text="Button" 
      android:textSize="20sp" /> 
    </GridLayout> 

</RelativeLayout> 

+0

嘗試閱讀[GridLayout](https://developer.android.com/reference/android/widget/GridLayout.html)上的文檔。它看起來像'過量空間分佈'部分,它討論了靈活性和不靈活性,特別是'爲了防止柱子拉伸,確保柱子中的一個部件沒有定義重量或重力。' – AChez9

回答

0

看你的佈局,我看不出有什麼問題。我沒有確切的設備,但我無法重現此問題。

但是,解決該問題的一種方法是使用ConstraintLayout而不是GridLayout。這還帶來了額外的好處,即允許您在沒有weight(GridLayout僅支持API 21+)的情況下獲得均勻大小的按鈕。

這裏有一個ConstraintLayout,做同樣的事情作爲網格:

<android.support.constraint.ConstraintLayout 
     android:id="@+id/gridLayout" 
     android:layout_width="match_parent" 
     android:layout_height="300dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignBottom="@+id/again"> 

     <Button 
      android:id="@+id/button4" 
      android:tag="0" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:textSize="20sp" 
      android:text="Button" 
      android:background="@color/colorPrimary" 
      app:layout_constraintTop_toTopOf="parent" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toLeftOf="@+id/button3" 
      app:layout_constraintBottom_toTopOf="@+id/button2"/> 

     <Button 
      android:id="@+id/button3" 
      android:tag="1" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:textSize="20sp" 
      android:text="Button" 
      android:background="@android:color/holo_blue_dark" 
      app:layout_constraintTop_toTopOf="parent" 
      app:layout_constraintLeft_toRightOf="@+id/button4" 
      app:layout_constraintRight_toRightOf="parent" 
      app:layout_constraintBottom_toTopOf="@+id/button"/> 

     <Button 
      android:id="@+id/button2" 
      android:tag="2" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:textSize="20sp" 
      android:text="Button" 
      android:background="@android:color/holo_green_light" 
      app:layout_constraintTop_toBottomOf="@+id/button4" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toLeftOf="@+id/button" 
      app:layout_constraintBottom_toBottomOf="parent"/> 

     <Button 
      android:id="@+id/button" 
      android:tag="3" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:textSize="20sp" 
      android:text="Button" 
      android:background="@android:color/holo_red_light" 
      app:layout_constraintTop_toBottomOf="@+id/button3" 
      app:layout_constraintLeft_toRightOf="@+id/button2" 
      app:layout_constraintRight_toRightOf="parent" 
      app:layout_constraintBottom_toBottomOf="parent"/> 

    </android.support.constraint.ConstraintLayout> 

如果你想獲得真正走進它,你很可能有一個頂層ConstraintLayout取代你的整個佈局,避免所有視圖築巢。但是你可以從這個開始。

+0

也許其他答案也是正確的,但這是我使用的第一個答案,它解決了我的問題。謝謝 :) – Adomas

0

從您發佈的圖片中,似乎四個按鈕顯示一些靜態數據。

刪除視圖的不必要的屬性。

您可以簡單地使用表格佈局來顯示按鈕並拉伸佈局中的按鈕。 下面的按鈕修改後的代碼:

<TableLayout 
     android:id="@+id/gridLayout" 
     android:layout_width="match_parent" 
     android:layout_height="300dp" 
     android:layout_alignBottom="@+id/again" 
     android:columnCount="2" 
     android:rowCount="2" 
     android:stretchColumns="*" 
     > 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      > 

     <Button 
      android:id="@+id/button4" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:background="@color/colorPrimary" 
      android:onClick="pressed" 
      android:tag="0" 
      android:text="Button" 
      android:textSize="20sp" /> 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@android:color/holo_blue_dark" 
      android:onClick="pressed" 
      android:tag="1" 
      android:text="Button" 
      android:textSize="20sp" /> 
     </TableRow> 

     <TableRow 

      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1"> 
     <Button 
      android:id="@+id/button2" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@android:color/holo_green_light" 
      android:onClick="pressed" 
      android:tag="2" 
      android:text="Button" 
      android:textSize="20sp" /> 

     <Button 
      android:id="@+id/button" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@android:color/holo_red_light" 
      android:onClick="pressed" 
      android:tag="3" 
      android:text="Button" 
      android:textSize="20sp" /> 
     </TableRow> 
    </TableLayout> 

而且還對不同的手機推出的時候不硬編碼的高度,更好的用戶界面體驗。

0

您正在嘗試使用API​​ 21中引入的GridLayout權重,但它們看起來不適用於您的API 21設備或我的API 21仿真器。他們在我的API 24設備和仿真器上工作。

支持庫似乎有更好的實現GridLayout - 至少有一個按預期使用權重,但您必須對代碼進行一些細微更改。以下是我想出了:

首先,GridLayout添加支持庫到你gradle這個文件:

compile 'com.android.support:gridlayout-v7:25.4.0' 

您可能需要一個不同的版本取決於其他要求。

這是一個可操作的XML文件,我已經簡化了您的文件。你會發現V7支持GridLayout

<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/gridLayout" 
    android:layout_width="match_parent" 
    android:layout_height="300dp" 
    app:columnCount="2" 
    app:rowCount="2"> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:background="@color/colorPrimary" 
     android:tag="0" 
     android:text="Button" 
     android:textSize="20sp" 
     app:layout_columnWeight="1" 
     app:layout_gravity="fill" 
     app:layout_rowWeight="1" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_blue_dark" 
     android:tag="1" 
     android:text="Button" 
     android:textSize="20sp" 
     app:layout_columnWeight="1" 
     app:layout_gravity="fill" 
     app:layout_rowWeight="1" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_green_light" 
     android:tag="2" 
     android:text="Button" 
     android:textSize="20sp" 
     app:layout_columnWeight="1" 
     app:layout_gravity="fill" 
     app:layout_rowWeight="1" /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_red_light" 
     android:tag="3" 
     android:text="Button" 
     android:textSize="20sp" 
     app:layout_columnWeight="1" 
     app:layout_gravity="fill" 
     app:layout_rowWeight="1" /> 
</android.support.v7.widget.GridLayout> 

這裏是什麼樣子:

enter image description here

正如你可以看到,支持庫實施GridLayout作品佈局。然而,我認爲你可能會得到很好的服務,但是,如OP所述,以不同的方式來做到這一點。