Android中的GridLayout为什么不自动调整大小

克期 2015-12-07 04:42:14
请教各位大牛,小弟新手,在学习GridLayout控件。遇到一个很奇怪的问题。

我想用GridLayout做一个简单的按键布局。预想的结果是下面这幅图(取自Android Studio中的预览效果)


也就是十个按键,能够均分屏幕。结果下载到手机上,发现均分不了。
手机竖屏显示时,最后一列按钮只显示了一点点,也就是只显示了4列,应该是显示5列的。


手机横屏蔽显示时,发现几个按钮不自己拉伸填满整个屏幕。


我在网上拷过别人的布局代码下来,发现也是这种效果。请问大牛们,是怎么回事呢?

谢谢各位,以下是我的布局代码.


<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:rowCount="5"
android:columnCount="5" >
  <Button
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="1"/>
  <Button
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="2"/>
  <Button
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="1"/>
  <Button
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="2"/>  
<Button
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="1"/>
<Button
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="2"/>  
<Button
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="1"/>
<Button
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="2"/>  
<Button
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="1"/>
<Button
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:text="2"/>
</GridLayout>
...全文
1245 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jklwan 2015-12-08
  • 打赏
  • 举报
回复
用兼容库,用兼容控件android.support.v7.widget.GridLayout,然后加上xmlns:app="http://schemas.android.com/apk/res-auto",使用app:layout_columnWeight="1",app:layout_rowWeight="1"
sanxiaochengyu 2015-12-08
  • 打赏
  • 举报
回复
为每一个希望平均分配的行或列分别指定 android:layout_columnWeight="1" android:layout_rowWeight="1" 这个是要在API 21也上才有效果
哎,真难 2015-12-07
  • 打赏
  • 举报
回复
引用 3 楼 lishutong_it 的回复:
[quote=引用 2 楼 heaimnmn 的回复:] layout_rowWeight这个地方应该写宽度layout_weight,
将Button加入GridLayout时,不支持layout_weight[/quote]为什么要用button了,image就可以了
克期 2015-12-07
  • 打赏
  • 举报
回复
引用 1 楼 crazymaple213 的回复:
android:orientation="horizontal" 改为android:orientation="vercital"
没效果,我同时把android:columnCount改成2了,整个页面还是有一大片空白。
克期 2015-12-07
  • 打赏
  • 举报
回复
引用 2 楼 heaimnmn 的回复:
layout_rowWeight这个地方应该写宽度layout_weight,
将Button加入GridLayout时,不支持layout_weight
哎,真难 2015-12-07
  • 打赏
  • 举报
回复
layout_rowWeight这个地方应该写宽度layout_weight,
crazymaple213 2015-12-07
  • 打赏
  • 举报
回复
android:orientation="horizontal" 改为android:orientation="vercital"

80,354

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧