放入 GalleryView 中的膨胀 ImageView 大小不正确

发布于 2024-10-07 17:59:45 字数 1081 浏览 0 评论 0原文

我正在尝试膨胀一个 ImageView 来缩放可以在 GalleryView 中显示的 Drawable 。我的代码来膨胀视图似乎工作正常,除了 ImageView 的属性没有应用。具体来说,膨胀的 ImageView 没有我通过 XML 中的 android:layout 参数为其设置的宽度/高度。

有人可以告诉我我做错了什么吗?

我想以 dp 为单位设置图像的宽度/高度,以便它在多个屏幕 dpi 上保持正确的尺寸并支持 Android 1.5+。因此,我不能使用类似的东西:

i.setLayoutParams(new Gallery.LayoutParams(150, 116)

我的布局定义是:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="150dp" android:layout_height="116dp"
    android:background="@drawable/gallery_item_background"
    android:scaleType="fitXY" />
</ImageView>

我用来膨胀 ImageView 的代码片段是:

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        ImageView i = (ImageView) inflater.inflate(R.layout.gallery_item, null);
        i.setImageResource(mImageIds.get(position));
        i.setScaleType(ImageView.ScaleType.FIT_XY);

        return i;
    }

I am trying to inflate an ImageView that scales a Drawable that I can display in a GalleryView. My code to inflate the view seems to work fine, except that the attributes of the ImageView are not applied. Specifically, the inflated ImageView does not have the width/height that I set for it via the android:layout params in XML.

Can someone show me what I'm doing wrong?

I want to set the width/height of the image in dp, so that it is the correct size across multiple screen dpis and support Android 1.5+. As a result I cannot use something like:

i.setLayoutParams(new Gallery.LayoutParams(150, 116)

My layout definition is:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="150dp" android:layout_height="116dp"
    android:background="@drawable/gallery_item_background"
    android:scaleType="fitXY" />
</ImageView>

And the snippet I am using to inflate the ImageView is:

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        ImageView i = (ImageView) inflater.inflate(R.layout.gallery_item, null);
        i.setImageResource(mImageIds.get(position));
        i.setScaleType(ImageView.ScaleType.FIT_XY);

        return i;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她比我温柔 2024-10-14 17:59:45

诀窍就是使用以下版本的 inflate()

inflater.inflate(R.layout.gallery_item, parent, false);

最后两个参数是强制性的。如果您将“null”作为父级传递,则 inflater 不知道要创建什么类型的布局参数,因此会忽略所有 android:layout_ XML 属性。最后一个参数只是告诉充气器不要立即将充气视图添加到父视图中。如果您传递 true (至少在适配器的 getView() 方法中),则会发生不好的事情。

The trick is simply to use the following version of inflate():

inflater.inflate(R.layout.gallery_item, parent, false);

The last two parameters are mandatory. If you pass "null" as the parent, the inflater does not know what type of layout parameters to create and therefore ignores all the android:layout_ XML attributes. The last parameter simply tells the inflater to not add the inflated view to the parent right away. If you pass true (at least inside an Adapter's getView() method), bad things will happen.

一张白纸 2024-10-14 17:59:45

我不确定但这可能有用。在你的xml中给你的imageview一个id(假设它是“@+id/image”:

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = getLayoutInflater();
    View view = (View) inflater.inflate(R.layout.gallery_item, null);
    ImageView i = (ImageView) view.findViewById(R.id.image);
    i.setImageResource(mImageIds.get(position));
    i.setScaleType(ImageView.ScaleType.FIT_XY);

    return view;
}

顺便说一句,你应该优化它检查convertView是否为null以便回收视图。检查这个:http://code.google.com/events/io/2009/sessions/TurboChargeUiAndroidFast.html

I'm not sure but this might work. Give an id to your imageview in your xml (say it is "@+id/image":

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = getLayoutInflater();
    View view = (View) inflater.inflate(R.layout.gallery_item, null);
    ImageView i = (ImageView) view.findViewById(R.id.image);
    i.setImageResource(mImageIds.get(position));
    i.setScaleType(ImageView.ScaleType.FIT_XY);

    return view;
}

By the way you should optimize it checking if convertView is null in order to recycle views. Check this: http://code.google.com/events/io/2009/sessions/TurboChargeUiAndroidFast.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文