放入 GalleryView 中的膨胀 ImageView 大小不正确
我正在尝试膨胀一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
诀窍就是使用以下版本的
inflate()
:最后两个参数是强制性的。如果您将“null”作为父级传递,则 inflater 不知道要创建什么类型的布局参数,因此会忽略所有
android:layout_
XML 属性。最后一个参数只是告诉充气器不要立即将充气视图添加到父视图中。如果您传递 true (至少在适配器的 getView() 方法中),则会发生不好的事情。The trick is simply to use the following version of
inflate()
: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'sgetView()
method), bad things will happen.我不确定但这可能有用。在你的xml中给你的imageview一个id(假设它是“@+id/image”:
顺便说一句,你应该优化它检查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":
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