安卓:GridView的宽度不包装内容? [英] Android: GridView width not wrapping to content?

查看:123
本文介绍了安卓:GridView的宽度不包装内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示在对话框一个GridView。尽管我所有的努力,在GridView宽度不断增大,而不是包装到列到整个屏幕。布局和图像描绘问题如下(我设置背景颜色为GridView来说明问题)。

I am trying to display a GridView in a Dialog. Despite all my efforts, the GridView width grows to the entire screen, instead of wrapping to the columns. The layout and an image depicting the issue are below (I set a background color for the GridView to illustrate the issue).

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/colorgridview"
    android:background="#FF00CCBB"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:numColumns="4"
    android:verticalSpacing="5dp"
    android:horizontalSpacing="5dp"
    android:columnWidth="70dp"
    android:stretchMode="none"
/>

推荐答案

我知道这个帖子是有点过时。但是,如果有人需要一个解决的办法,这个答案可能会派上用场。

I know that this post is a bit outdated. But if someone needs a solution to this, this answer may come in handy.

这是可能的屏幕测量后,设置视图的宽度。

It is possible to set the view's width after screen measurement.

要做到这一点:

让你的类实现OnGlobalLayoutListener。 当onGlobalLayout方法被称为屏幕被测量。我们可以在这里做我们的法宝。 GridView.getLayoutParams()。宽度= ....

Let your class implement the OnGlobalLayoutListener. The screen is measured when the onGlobalLayout method is called. We can do our magic here. GridView.getLayoutParams().width = ....

编辑: 我不是如何添加onGlobalLayoutListener很清楚。见plugmind的帖子中,他展示了如何添加它。 <一href="http://stackoverflow.com/questions/3581753/cant-figure-out-how-to-get-view-layout-width-height">Can't弄清楚如何获得视图/布局宽/高

edit: I wasn't very clear on how to add the onGlobalLayoutListener. See plugmind's post, he shows how to add it. Can't figure out how to get view/layout width/height

亲切的问候, 布拉姆

这篇关于安卓:GridView的宽度不包装内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆