2017-02-11 59 views
9

我有LinearLayoutTextView,GridLayout和另一個TextView。它在許多現代手機和平板電腦上正確顯示,但它完全無法在像Nexus S這樣的小型顯示器上顯示。請參閱下面的圖片。GridLayout在小型顯示器上坍塌

enter image description here enter image description here

我試圖縮小字體大小,除去9補丁背景,甚至刪除所有的利潤。當有一些利潤時,我可以看到數字7。然後我最小化了它們,我可以看到四條線,第一個按鈕的顯示次數是顯示的兩倍。我不明白爲什麼,它應該有wrap_content寬度。

我試圖儘量減少代碼,並將其減少到GridLayout與單行,它仍然不適合屏幕的寬度。

這是怎麼回事呢?

更新:

  • 420x800 MDPI工作
  • 420x800華電國際被打破
  • 540x920華電國際工程
  • 720x1280華電國際工程

更新2:

的Nexus 5X有字體爲38sp時的相同問題r更大。有趣的是,工作室的設計師顯示正確的佈局,但兩者的裝置/模擬器顯示損壞的視圖和以下錯誤:

D/android.support.v7.widget.GridLayout: vertical constraints: y1-y0>=197, y2-y1>=197, y3-y2>=197, y4-y3>=197, y4-y0<=785 are inconsistent; permanently removing: y4-y0<=785. 

calc.xml:

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<!--Display row--> 
<TextView 
    android:id="@+id/assignment" 
    android:text="50 + 40 = 90" 
    style="@style/Formula" 
    android:focusable="false" 
    android:layout_width="match_parent" 
    android:layout_height="80dp" 
    tools:ignore="HardcodedText" /> 

<android.support.v7.widget.GridLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    app:columnCount="4" 
    app:rowCount="4"> 

    <!-- row with 7-9,+ --> 

    <Button 
     android:id="@+id/digit7" 
     android:text="7" 
     style="@style/KeypadLeftButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     app:layout_rowWeight="1" 
     app:layout_columnWeight="1" 
     tools:ignore="HardcodedText" /> 

    <Button 
     android:id="@+id/digit8" 
     android:text="8" 
     style="@style/KeypadButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     app:layout_rowWeight="1" 
     app:layout_columnWeight="1" 
     tools:ignore="HardcodedText" /> 

    <Button 
     android:id="@+id/digit9" 
     android:text="9" 
     style="@style/KeypadButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     app:layout_rowWeight="1" 
     app:layout_columnWeight="1" 
     tools:ignore="HardcodedText" /> 

    <Button 
     android:id="@+id/buttonPlus" 
     android:text="+" 
     style="@style/KeypadRightButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_rowWeight="0.5" 
     app:layout_columnWeight="1" 
     tools:ignore="HardcodedText" /> 

    <!--row with 4-6, - --> 

    <Button 
     android:id="@+id/digit4" 
     android:text="4" 
     style="@style/KeypadLeftButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     app:layout_rowWeight="1" 
     app:layout_columnWeight="1" 
     tools:ignore="HardcodedText" /> 

    <Button 
     android:id="@+id/digit5" 
     android:text="5" 
     style="@style/KeypadButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     app:layout_rowWeight="1" 
     app:layout_columnWeight="1" 
     tools:ignore="HardcodedText" /> 

    <Button 
     android:id="@+id/digit6" 
     android:text="6" 
     style="@style/KeypadButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     app:layout_rowWeight="1" 
     app:layout_columnWeight="1" 
     tools:ignore="HardcodedText" /> 

    <Button 
     android:id="@+id/buttonMinus" 
     android:text="-" 
     style="@style/KeypadRightButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_rowWeight="0.5" 
     app:layout_columnWeight="1" 
     tools:ignore="HardcodedText" /> 

    <!--row with 1-3, * --> 

    <Button 
     android:id="@+id/digit1" 
     android:text="1" 
     style="@style/KeypadLeftButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     app:layout_rowWeight="1" 
     app:layout_columnWeight="1" 
     tools:ignore="HardcodedText" /> 

    <Button 
     android:id="@+id/digit2" 
     android:text="2" 
     style="@style/KeypadButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     app:layout_rowWeight="1" 
     app:layout_columnWeight="1" 
     tools:ignore="HardcodedText" /> 

    <Button 
     android:id="@+id/digit3" 
     android:text="3" 
     style="@style/KeypadButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     app:layout_rowWeight="1" 
     app:layout_columnWeight="1" 
     tools:ignore="HardcodedText" /> 

    <Button 
     android:id="@+id/buttonMultiply" 
     android:text="\u22C5" 
     style="@style/KeypadRightButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_rowWeight="0.5" 
     app:layout_columnWeight="1" 
     tools:ignore="HardcodedText" /> 

    <!--row with 0, backspace and/--> 

    <Button 
     android:id="@+id/digit0" 
     android:text="0" 
     style="@style/KeypadLeftButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_columnWeight="1" 
     app:layout_gravity="fill_horizontal" 
     app:layout_rowWeight="1" 
     tools:ignore="HardcodedText" /> 

    <Button 
     android:id="@+id/buttonBackspace" 
     android:text="←" 
     style="@style/KeypadButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_gravity="fill_vertical" 
     app:layout_columnSpan="2" 
     app:layout_rowWeight="0.5" 
     app:layout_columnWeight="1" 
     tools:ignore="HardcodedText" /> 

    <Button 
     android:id="@+id/buttonDivide" 
     android:text=":" 
     style="@style/KeypadRightButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_rowWeight="0.5" 
     app:layout_columnWeight="1" 
     tools:ignore="HardcodedText" /> 
</android.support.v7.widget.GridLayout> 

<!--row with button submit --> 

<Button 
    android:id="@+id/buttonSubmit" 
    android:text="@string/action_next_formula" 
    style="@style/KeypadNextButton" 
    android:layout_width="match_parent" 
    android:layout_height="80dp" 
    app:layout_gravity="fill_horizontal"/> 
</LinearLayout> 

風格:

<style name="Formula"> 
    <item name="android:textSize">@dimen/calc_button_text</item> 
    <item name="android:textColor">@color/gray_35</item> 
    <item name="android:gravity">center</item> 
    <item name="android:background">@drawable/lcd</item> 
    <item name="android:layout_marginTop">@dimen/calc_big_margin</item> 
    <item name="android:layout_marginBottom">@dimen/calc_superbig_margin</item> 
    <item name="android:layout_marginLeft">@dimen/calc_big_margin</item> 
    <item name="android:layout_marginRight">@dimen/calc_big_margin</item> 
</style> 

<style name="KeypadButton" parent="@style/Widget.AppCompat.Button"> 
    <item name="android:textSize">@dimen/calc_button_text</item> 
    <item name="android:textColor">@color/white</item> 
    <!--<item name="android:background">@drawable/tl_2</item>--> 
    <item name="android:layout_marginLeft">@dimen/calc_small_margin</item> 
    <item name="android:layout_marginBottom">@dimen/calc_small_margin</item> 
</style> 

<style name="KeypadLeftButton" parent="@style/KeypadButton"> 
    <item name="android:layout_marginLeft">@dimen/calc_big_margin</item> 
</style> 

<style name="KeypadRightButton" parent="@style/KeypadButton"> 
    <item name="android:layout_marginRight">@dimen/calc_big_margin</item> 
</style> 

<style name="KeypadNextButton" parent="@style/Widget.AppCompat.Button.Colored"> 
    <item name="android:background">@drawable/tl_next</item> 
    <item name="android:textSize">@dimen/calc_button_text</item> 
    <item name="android:layout_marginTop">@dimen/calc_big_margin</item> 
    <item name="android:layout_marginBottom">@dimen/calc_big_margin</item> 
    <item name="android:layout_marginLeft">@dimen/calc_big_margin</item> 
    <item name="android:layout_marginRight">@dimen/calc_big_margin</item> 
</style> 

夢詩:

<dimen name="calc_button_text">14sp</dimen> 
<dimen name="calc_superbig_margin">2dp</dimen> 
<dimen name="calc_big_margin">1dp</dimen> 
<dimen name="calc_small_margin">0dp</dimen> 

更新16th feb

Manjunath Prabhakar表示,麻煩可能與權重有關。所以我試圖刪除所有的重量屬性,現在佈局更有意義。但我還沒有修復它。我會考慮重寫佈局以嵌套LinearLayouts作爲退後一步,因此我會爲其他建議打開賞金。

enter image description here

+0

我認爲你需要爲hdpi設備分開佈局。 –

+0

是的。但我必須先讓它在hdpi上工作。當我移除權重時,它可以正確顯示,但單元格不佔用所有可用空間。由於只有最後一個單元展開,因此不能將layout_gravity屬性設置爲fill_horizo​​ntal | fill_vertical。 –

+0

我認爲你必須讓你的物品寬度match_parent,以最大限度地利用佈局的重量!你可以試試這個嗎? – Mohamed

回答

6

我猜是的GridLayout支持版本的錯誤。

看看this(即目前分配):我認爲這是與你有關的Nexus 5X字號的行爲問題。一般來說,我發現了很多相關的錯誤here(有關GridLayout寬度測量)。

我會盡量給你一個解決方法。

我使用com.android.support:gridlayout-v7:25.1.1

我想你可以解決你的佈局問題(無論是在舊的手機對準和Nexus 5X保護字體大小)做了這種方式:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <!--Display row--> 
    <TextView 
     android:id="@+id/assignment" 
     android:text="50 + 40 = 90" 
     style="@style/Formula" 
     android:focusable="false" 
     android:layout_width="match_parent" 
     android:layout_height="80dp" 
     tools:ignore="HardcodedText" /> 

    <android.support.v7.widget.GridLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     app:columnCount="4" 
     app:rowCount="4"> 

     <!-- row with 7-9,+ --> 

     <LinearLayout 
      app:layout_gravity="fill_horizontal" 
      app:layout_columnSpan="4" 
      android:orientation="horizontal" 
      app:layout_rowWeight="1"> 
     <Button 
      android:id="@+id/digit7" 
      android:text="7" 
      style="@style/KeypadLeftButton" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      app:layout_rowWeight="1" 
      app:layout_columnWeight="1" 
      tools:ignore="HardcodedText" /> 

     <Button 
      android:id="@+id/digit8" 
      android:text="8" 
      style="@style/KeypadButton" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      app:layout_rowWeight="1" 
      app:layout_columnWeight="1" 
      tools:ignore="HardcodedText" /> 

     <Button 
      android:id="@+id/digit9" 
      android:text="9" 
      style="@style/KeypadButton" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      app:layout_rowWeight="1" 
      app:layout_columnWeight="1" 
      tools:ignore="HardcodedText" /> 

     <Button 
      android:id="@+id/buttonPlus" 
      android:text="+" 
      style="@style/KeypadRightButton" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      app:layout_rowWeight="0.5" 
      app:layout_columnWeight="1" 
      tools:ignore="HardcodedText" /> 

     </LinearLayout> 

     <!--row with 4-6, - --> 
     <LinearLayout 
      app:layout_gravity="fill_horizontal" 
      app:layout_columnSpan="4" 
      android:orientation="horizontal" 
      app:layout_rowWeight="1"> 
     <Button 
      android:id="@+id/digit4" 
      android:text="4" 
      style="@style/KeypadLeftButton" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      app:layout_rowWeight="1" 
      app:layout_columnWeight="1" 
      tools:ignore="HardcodedText" /> 

     <Button 
      android:id="@+id/digit5" 
      android:text="5" 
      style="@style/KeypadButton" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      app:layout_rowWeight="1" 
      app:layout_columnWeight="1" 
      tools:ignore="HardcodedText" /> 

     <Button 
      android:id="@+id/digit6" 
      android:text="6" 
      style="@style/KeypadButton" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      app:layout_rowWeight="1" 
      app:layout_columnWeight="1" 
      tools:ignore="HardcodedText" /> 

     <Button 
      android:id="@+id/buttonMinus" 
      android:text="-" 
      style="@style/KeypadRightButton" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      app:layout_rowWeight="0.5" 
      app:layout_columnWeight="1" 
      tools:ignore="HardcodedText" /> 

     </LinearLayout> 
     <!--row with 1-3, * --> 

     <LinearLayout 
      app:layout_gravity="fill_horizontal" 
      app:layout_columnSpan="4" 
      android:orientation="horizontal" 
      app:layout_rowWeight="1"> 
     <Button 
      android:id="@+id/digit1" 
      android:text="1" 
      style="@style/KeypadLeftButton" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      app:layout_rowWeight="1" 
      app:layout_columnWeight="1" 
      tools:ignore="HardcodedText" /> 

     <Button 
      android:id="@+id/digit2" 
      android:text="2" 
      style="@style/KeypadButton" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      app:layout_rowWeight="1" 
      app:layout_columnWeight="1" 
      tools:ignore="HardcodedText" /> 

     <Button 
      android:id="@+id/digit3" 
      android:text="3" 
      style="@style/KeypadButton" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      app:layout_rowWeight="1" 
      app:layout_columnWeight="1" 
      tools:ignore="HardcodedText" /> 

     <Button 
      android:id="@+id/buttonMultiply" 
      android:text="\u22C5" 
      style="@style/KeypadRightButton" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      app:layout_rowWeight="0.5" 
      app:layout_columnWeight="1" 
      tools:ignore="HardcodedText" /> 

     </LinearLayout> 
     <!--row with 0, backspace and/--> 

     <LinearLayout 
      app:layout_gravity="fill_horizontal" 
      app:layout_columnSpan="4" 
      android:orientation="horizontal" 
      app:layout_rowWeight="1" 
      android:weightSum="4"> 
     <Button 
      android:id="@+id/digit0" 
      android:text="0" 
      style="@style/KeypadLeftButton" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      app:layout_columnWeight="1" 
      app:layout_gravity="fill_horizontal" 
      app:layout_rowWeight="1" 
      tools:ignore="HardcodedText" 
      android:layout_weight="1"/> 

     <Button 
      android:id="@+id/buttonBackspace" 
      android:text="←" 
      style="@style/KeypadButton" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      app:layout_gravity="fill_vertical" 
      app:layout_columnSpan="2" 
      app:layout_rowWeight="0.5" 
      app:layout_columnWeight="1" 
      tools:ignore="HardcodedText" 
      android:layout_weight="2"/> 

     <Button 
      android:id="@+id/buttonDivide" 
      android:text=":" 
      style="@style/KeypadRightButton" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      app:layout_rowWeight="0.5" 
      app:layout_columnWeight="1" 
      tools:ignore="HardcodedText" 
      android:layout_weight="1"/> 
     </LinearLayout> 
    </android.support.v7.widget.GridLayout> 

    <!--row with button submit --> 

    <Button 
     android:id="@+id/buttonSubmit" 
     android:text="NEXT" 
     style="@style/KeypadNextButton" 
     android:layout_width="match_parent" 
     android:layout_height="80dp" 
     app:layout_gravity="fill_horizontal"/> 
</LinearLayout> 

基本上,我所做的是包裝每組按鈕(除最後一行之外的四個按鈕)在LinearLayout中。

在這裏你可以看到它的外觀在兩款手機:

的Nexus 5 Nexus 5 screenshot

三星galaxy核心加號(舊的) Galaxy core plus screenshot

正如你所看到的,是一個問題最後一行的對齊方式。我認爲你可以嘗試解決這些按鈕與屬性和屬性的對齊問題。讓我知道,如果你想在這個最後的話題進一步改善。

希望這會有所幫助。

+0

謝謝,我會盡力的! –

+0

我有一段時間完全回顧你的答案。基本上你建議將GridLayout轉換爲一組LinearLayout。正如我早先在Manjunath的回答中所說的那樣,這會起作用,但我並不高興。 –

+1

是的,我知道這個實現不使用GridLayout應該提供的靈活性,它不應該是最好的方法之一。我只是想提供一種繞過問題的方法 –

2

有一些限制,並使用網格佈局時的限制。 這裏是鏈接到DOCUMENTATION

"GridLayout does not provide support for the principle of weight, as defined in weight. In general, it is not therefore possible to configure a GridLayout to distribute excess space in non-trivial proportions between multiple rows or columns ... For complete control over excess space distribution in a row or column; use a LinearLayout subview to hold the components in the associated cell group."

如果我是你,我會使用線性佈局或表格佈局。

希望這會有所幫助!

+0

有趣的一點。我試圖最近重寫代碼到ConstraintLayout,但我完全失敗了。我認爲第一個實現是使用LinearLayout,但我討厭深層嵌套。 Gridlayout應該具有更好的性能並消耗更少的資源。 –

+0

我刪除了所有重量屬性,並沒有足夠的空間用於所有按鈕,但現在看起來更好。我會發布更新的截圖。 –

+1

@Leos Literak:很高興聽到它幫助你解決你的問題!期待收到你的來信 –