充气的ImageView投入GalleryView是不正确的大小不正确、大小、ImageView、GalleryView

2023-09-13 01:25:32 作者:菊部地区有血

我试图夸大一个扩展的绘制对象,我可以在一个GalleryView显示ImageView的。我的code膨胀的观点似乎做工精细,除了ImageView的属性不适用。具体来说,膨胀ImageView的不具有宽度/高度,我为它通过Android设置:在XML布局PARAMS

有人能告诉我什么,我做错了什么?

我要设置DP图像的宽度/高度,使其在多个屏幕的DPI正确的尺寸,并支持Android 1.5+。因此,我不能使用这样的:

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

我的布局定义是:

 < XML版本=1.0编码=UTF-8&GT?;
< ImageView的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =150dp机器人:layout_height =116dp
    机器人:背景=@可绘制/ gallery_item_background
    机器人:scaleType =fitXY/>
< / ImageView的>
 

和我使用吹大的ImageView的片段是:

 公开查看getView(INT位置,查看convertView,ViewGroup中父){
        LayoutInflater充气= getLayoutInflater();
        ImageView的I =(ImageView的)inflater.inflate(R.layout.gallery_item,NULL);
        i.setImageResource(mImageIds.get(位置));
        i.setScaleType(ImageView.ScaleType.FIT_XY);

        返回我;
    }
 
英超3队将引进360全景系统 观赛体验或全面提升

解决方案

诀窍就是使用以下版本膨胀()

  inflater.inflate(R.layout.gallery_item,父母,假);
 

最后两个参数是强制性的。如果传递空作为父母,充气不知道是什么类型的布局参数来创建,因此忽略所有的安卓布局_ XML属性。最后一个参数只是告诉充气的充气观点不添加到父的时候了。如果你通过真正的(至少是内部的适配器的 getView()法),不好的事情会发生。

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;
    }

解决方案

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.