我试图在运行时动态添加按钮,因此设计阶段xml最小。所有按钮都是在运行时创建的。一切正常,直到四个按钮添加到一行。当第五个按钮添加到GridLayout时,它会出错。
当我添加第五个按钮时,我试图影响约束以避免出现以下错误: -
D/android.widget.GridLayout: horizontal constraints: x1-x0>=231, x2-x1>=231, x3-x2>=231, x4-x3>=231, x5-x4>=231, x5-x0<=1080 are inconsistent; permanently removing: x5-x0<=1080.
我的GridLayout中有五个按钮试图挤压自己,但它们不适合1080dp
,231dp*5=1155
。
1. 231dp
来自哪里?
日志输出显示Button.getMinimumWidth()
为231dp
(向右滚动以查看最后一个参数): -
D/CommandCenter: Btn <0> details:X<0.0>Y<802.0> || W<270>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<0> || MinW<231>
D/CommandCenter: Btn <1> details:X<270.0>Y<802.0> || W<270>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<270> || MinW<231>
D/CommandCenter: Btn <2> details:X<540.0>Y<802.0> || W<270>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<540> || MinW<231>
I/zygote: Waiting for a blocking GC Debugger
D/CommandCenter: Btn <3> details:X<810.0>Y<802.0> || W<270>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<810> || MinW<231>
//btnContainer --> GridLayout Object
//btn --> Button Object
//BTN_COLS --> Number of Buttons in one row
btn.setMinimumWidth(btnContainer.getWidth()/BTN_COLS);
3。检查最小宽度是否设置正确(向右滚动,日志行中的参数MinW):
D/CommandCenter: Btn <0> details:X<0.0>Y<802.0> || W<270>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<0> || MinW<216>
D/CommandCenter: Btn <1> details:X<270.0>Y<802.0> || W<270>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<270> || MinW<216>
D/CommandCenter: Btn <2> details:X<540.0>Y<802.0> || W<270>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<540> || MinW<216>
D/CommandCenter: Btn <3> details:X<810.0>Y<802.0> || W<270>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<810> || MinW<216>
不幸的是,按钮的最小宽度未用于解析(求解)GirdLayout
约束,但仍显示相同的错误,因为正在使用的宽度仍为231dp
而不是216dp
D/android.widget.GridLayout: horizontal constraints: x1-x0>=231, x2-x1>=231, x3-x2>=231, x4-x3>=231, x5-x4>=231, x5-x0<=1080 are inconsistent; permanently removing: x5-x0<=1080.
。
一切都变得混乱之后,这是事态:
D/CommandCenter: Btn <0> details:X<0.0>Y<802.0> || W<2391>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<0> || MinW<216>
D/CommandCenter: Btn <1> details:X<2391.0>Y<802.0> || W<2391>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<2391> || MinW<216>
D/CommandCenter: Btn <2> details:X<4782.0>Y<802.0> || W<2391>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<4782> || MinW<216>
D/CommandCenter: Btn <3> details:X<7173.0>Y<802.0> || W<2391>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<7173> || MinW<216>
D/CommandCenter: Btn <4> details:X<9564.0>Y<802.0> || W<2391>H<126> || PadL<31>PadR<31>PadT<25> || MarL<0>MarR<0>MarT<0> || L<9564> || MinW<216>