我有LinearLayout和TextView,GridLayout和另一个TextView。它被正确地显示在许多现代手机和平板电脑上,但在像Nexus这样的小型显示器上却完全失败,参见下图。


我试着缩小字体大小,删除9补丁背景,甚至删除所有边距。当有一些利润的时候,我可以看到只有digit7。然后我将它们最小化,我可以看到四行,它们的第一个按钮被渲染成显示器的两倍大。我不明白为什么,它应该有wrap_content宽度。
我尝试将代码最小化,并将其简化为带有单行的GridLayout,但它仍然不适合屏幕的宽度。
那是怎么回事?
更新:
更新2:
当字体为38 5x或更大时,Nexus5x也有相同的问题。有趣的是,Studio的设计器显示了正确的布局,但是设备/模拟器都显示了损坏的视图,并出现了以下错误:
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>更新2月16日
Manjunath说,问题可能与体重有关。所以我尝试删除所有的权重属性,现在布局更有意义了。不过,我还没修好呢。我会考虑将布局重写为嵌套的LinearLayouts作为后退的一步,因此我会对其他建议开放。

发布于 2017-02-15 11:58:36
我想这是GridLayout支持版本的一个bug。
看看这 (目前指定的):我认为这与您有关Nexus5X字体大小行为的问题有关。总的来说,我发现了很多与这里 (与GridLayout宽度测量相关的)相关的bug。
我试着给你个解决办法。
我在用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

三星星系核心+(旧版)

正如您所看到的,最后一行的对齐存在一个问题。我认为您可以尝试解决这些按钮的对齐问题,使用属性和属性。如果你想在最后一个话题上做进一步的改进,请告诉我。
希望这能有所帮助。
发布于 2017-02-15 11:43:11
在使用GridLayout时几乎没有限制和限制。下面是链接到文档的链接
"GridLayout不支持按权重定义的权重原则。因此,通常不可能将GridLayout配置为在多行或多列之间按非平凡的比例分配多余的空间.以便完全控制一行或列中多余的空间分布;使用LinearLayout子视图来保存相关单元格组中的组件。“
如果我是你,我会使用线性布局或表格布局。
希望这能有所帮助!
发布于 2020-06-30 14:40:28
虽然为时已晚,但同样的问题可以通过将layout_width和layout_height设置为GridLayout (AndroidX)中的按钮到0dp来解决。
<Button
android:id="@+id/digit8"
android:text="8"
style="@style/KeypadButton"
android:layout_width="0dp" <---
android:layout_height="0dp" <---
android:layout_weight="1"
app:layout_rowWeight="1"
app:layout_columnWeight="1"
tools:ignore="HardcodedText" />https://stackoverflow.com/questions/42173015
复制相似问题