Inflated ImageView to put in GalleryView isn#39;t the right size(放入 GalleryView 的膨胀 ImageView 尺寸不合适)
问题描述
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.
这篇关于放入 GalleryView 的膨胀 ImageView 尺寸不合适的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:放入 GalleryView 的膨胀 ImageView 尺寸不合适
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01