2017-02-11 26 views
9

Tôi có LinearLayout với TextView, GridLayout và một TextView khác. Nó được hiển thị chính xác trên nhiều điện thoại và máy tính bảng hiện đại nhưng hoàn toàn không hiển thị trên màn hình nhỏ như Nexus S. Xem hình ảnh sau đây.GridLayout bị thu nhỏ trên màn hình nhỏ

enter image description here enter image description here

Tôi cố gắng để giảm cỡ chữ, loại bỏ nền 9 miếng vá và thậm chí loại bỏ tất cả lợi nhuận. Tôi chỉ có thể thấy chữ số 7 khi có một số lợi nhuận. Sau đó, tôi giảm thiểu chúng và tôi có thể nhìn thấy bốn dòng với nút đầu tiên của họ được hiển thị gấp đôi so với hiển thị. Tôi không hiểu tại sao, nó sẽ có chiều rộng wrap_content.

Tôi đã cố gắng thu nhỏ mã và giảm mã xuống GridLayout bằng một hàng và nó vẫn không vừa với chiều rộng của màn hình.

Điều gì đang xảy ra ở đó?

Cập nhật:

  • 420x800 mdpi làm việc
  • 420x800 hdpi được chia
  • 540x920 hdpi làm việc
  • 720x1280 hdpi làm việc

Cập nhật 2:

5x Nexus có cùng một vấn đề khi phông chữ là 38sp o r lớn hơn. Điều thú vị là nhà thiết kế Studio cho thấy cách bố trí đúng nhưng cả hai thiết bị/giả lập cho thấy quan điểm hỏng và lỗi sau:

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> 

phong cách:

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

dimens:

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

Cập nhật 16th feb

Manjunath Prabhakar nói rằng sự cố có thể liên quan đến trọng số. Vì vậy, tôi đã cố gắng loại bỏ tất cả các thuộc tính trọng số và bố cục giờ đây có ý nghĩa hơn. Tôi chưa sửa nó. Tôi sẽ xem xét viết lại bố trí để lồng nhau LinearLayouts như là một bước trở lại do đó tôi làm cho tiền thưởng mở cho các đề xuất khác.

enter image description here

+0

Tôi nghĩ bạn cần đặt bố cục riêng cho thiết bị hdpi. –

+0

Đúng. Nhưng tôi phải làm cho nó hoạt động trên hdpi trước. Khi tôi loại bỏ trọng lượng, nó có thể hiển thị chính xác nhưng các ô không chiếm tất cả không gian có sẵn. Không đặt thuộc tính layout_gravity thành fill_horizontal | fill_vertical sẽ giúp, vì chỉ ô cuối cùng mở rộng. –

+0

Tôi nghĩ bạn phải làm cho các mặt hàng của bạn có chiều rộng match_parent để tận dụng tốt nhất trọng lượng bố cục! bạn có thể thử cái này không – Mohamed

Trả lời

6

Tôi đoán đó là lỗi của phiên bản hỗ trợ của GridLayout.

Hãy xem this (hiện tại là Được gán): Tôi nghĩ rằng nó liên quan đến câu hỏi của bạn về hành vi kích thước phông chữ Nexus 5X. Nói chung, tôi tìm thấy rất nhiều lỗi liên quan here (liên quan đến GridLayout đo chiều rộng).

Tôi sẽ cố gắng giải quyết cho bạn.

Tôi đang sử dụng com.android.support:gridlayout-v7:25.1.1.

Tôi nghĩ rằng bạn có thể giải quyết vấn đề bố trí của mình (cả sự liên kết trong điện thoại cũ và kích thước phông chữ trong Nexus 5X) làm theo cách này:

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

Về cơ bản, những gì tôi đã làm là để bọc từng nhóm nút (bốn nút ngoại trừ hàng cuối cùng) trong một LinearLayout.

Ở đây bạn có thể thấy nó trông như thế trong hai điện thoại:

Nexus 5 Nexus 5 screenshot

Samsung galaxy lõi cộng (cũ) Galaxy core plus screenshot

Như bạn thấy, có một vấn đề trong sự liên kết của hàng cuối cùng. Tôi nghĩ bạn có thể cố gắng giải quyết sự liên kết của các nút này với các thuộc tính và thuộc tính. Hãy cho tôi biết nếu bạn muốn cải thiện hơn nữa trong chủ đề cuối cùng này.

Hy vọng điều này sẽ hữu ích.

+0

Cảm ơn, tôi sẽ cố gắng! –

+0

Tôi đã có thời gian để xem lại câu trả lời của bạn hoàn toàn. Về cơ bản, bạn đang đề xuất chuyển đổi GridLayout thành một tập hợp LinearLayout. Điều này sẽ làm việc nhưng tôi không phải là rất hạnh phúc, như tôi đã nhận xét trước đó tại câu trả lời của Manjunath. –

+1

Vâng, tôi biết việc triển khai này không sử dụng tính linh hoạt mà GridLayout phải cung cấp, nó không phải là một trong những phương pháp tốt nhất. Tôi chỉ muốn cung cấp một cách để bỏ qua các vấn đề –

2

Có một số hạn chế và hạn chế khi sử dụng GridLayout. Dưới đây là liên kết để 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."

Nếu tôi là bạn, tôi sẽ sử dụng tuyến tính bố trí hoặc bố trí bảng.

Hy vọng điều này sẽ hữu ích!

+0

Điểm thú vị. Tôi đã cố gắng viết lại mã gần đây để ConstraintLayout nhưng tôi đã thất bại hoàn toàn. Tôi nghĩ rằng việc thực hiện đầu tiên đã được sử dụng LinearLayout nhưng tôi ghét rằng sâu làm tổ. Gridlayout nên có hiệu suất tốt hơn và tiêu thụ ít tài nguyên hơn. –

+0

Tôi đã xóa tất cả các thuộc tính trọng số và không có đủ dung lượng cho tất cả các nút nhưng giờ đây có vẻ tốt hơn. Tôi sẽ đăng ảnh chụp màn hình cập nhật. –

+1

@Leos Literak: Tốt khi biết rằng nó giúp bạn giải quyết vấn đề của mình! Rất mong nhận được thông tin từ bạn –

Các vấn đề liên quan