如何强制 GridView 使用整个屏幕(无论显示大小)?
我有以下布局文件,其中有一个 GridView
和一个 ImageView
作为背景。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginRight="-70dp"
android:layout_marginBottom="-50dp"
android:src="@drawable/s_background"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
</LinearLayout>
</FrameLayout>
这是我用于网格每个“单元格”中实际项目的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/cardInGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:singleLine="true"
android:textSize="40sp"
android:textColor="#660099"
android:typeface="serif"/>
</LinearLayout>
我现在在我的设备上看到以下内容:
有什么方法可以使 GridView 中的每个项目变大,使其适合屏幕大小,并且显示屏底部没有未使用的空白区域?
这在模拟器上工作得很好,但在设备上屏幕分辨率更高,因此底部有空白。
非常感谢
I've got the following layout file, which has a GridView
and an ImageView
behind that as the background.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginRight="-70dp"
android:layout_marginBottom="-50dp"
android:src="@drawable/s_background"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
</LinearLayout>
</FrameLayout>
And this is the layout I use for the actual item in each "cell" of the grid :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/cardInGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:singleLine="true"
android:textSize="40sp"
android:textColor="#660099"
android:typeface="serif"/>
</LinearLayout>
I'm seeing the following on my device at the moment :
Is there any way of making each item in the GridView larger, so it fits the size of the screen and I don't have un-used white space at the bottom of the display?
This works fine on an emulator, but on a device the screen resolution is higher, hence getting the white space at the bottom.
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这会运作良好。不要忘记垂直间距。
This will work well. Don't forget about vertical spacing.
不是自动的。
特别是,您的单元格是文本。 Android 无法完全猜测文本应该有多大才能实现您的目标,尤其是在考虑自动换行的情况下。
GridView 的要点是在没有足够的数据来填充屏幕的情况下,在显示底部有“未使用的空白区域”,以便它可以灵活地处理多种屏幕尺寸和如果有更多数据,还可以适应滚动。如果您的目标是类似于仪表板模式,请考虑使用
DashboardLayout
或沿着这些思路的东西。Not automatically.
In particular, your cells are text. Android is not exactly in position to guess how big the text should be to accomplish your aims, particularly once you take word-wrap into account.
The point of
GridView
is to have "un-used white space at the bottom of the display", if you do not have enough data to fill the screen, so that it can flexibly handle multiple screen sizes and also accommodate scrolling if there is more data. If you are aiming for something akin to the dashboard pattern, consider usingDashboardLayout
or something along those lines.您尝试过
android:layout_weight="1"
吗?或者
希望有帮助?
Have you tried
android:layout_weight="1"
?Or
Hope that helps?