【发布时间】:2014-11-28 02:57:12
【问题描述】:
这是我从其中膨胀图像视图的布局文件。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.Tamaritz.dailyselfie.MainActivity" >
<ImageView
android:id="@+id/selfieImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/abc_ab_bottom_solid_dark_holo" />
<TextView
android:id="@+id/timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/selfieImage"
/>
这是我在基本适配器中的代码,它将将此布局文件扩展为具有视图持有者类(内部静态类)的视图对象
static class ViewHolder {
ImageView selfieImage;
TextView timestamp;
public ViewHolder(View selfieLayout) {
selfieImage = (ImageView) selfieLayout.findViewById(R.id.selfieImage);
timestamp = (TextView) selfieLayout.findViewById(R.id.timestamp);
}
}
....
public View getView(int position, View convertView, ViewGroup parent) {
View selfieImage = convertView;
ViewHolder holder = null;
if(selfieImage == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
selfieImage = inflater.inflate(R.layout.activity_main, parent, false);
holder = new ViewHolder(selfieImage);
int height = holder.selfieImage.getHeight();
int width = holder.selfieImage.getWidth();
selfieImage.setTag(holder);
}
}
我基本上想做的是利用 Android View holder 的模式。如果convertView 为null,则表示它之前没有被充气(不能重用),所以我们应该采取步骤充气。我将膨胀的视图传递给构造函数,以便视图能够找到它的图像视图和文本视图。我的问题是在图像视图被膨胀并附加到视图支架之后,为什么它的高度和宽度为 0?(知道这一点,因为我逐步完成了我的代码)。这些值是实际值很重要,因为我需要在http://developer.android.com/training/camera/photobasics.html 的 setPic 方法中使用它们。
我查看了其他可能的线程来解决我的问题。 https://stackoverflow.com/questions/20434257/gridview-setadapter-imageview-width-and-height-0 的人也有类似的问题,但从未得到答案。 这个Why does my FrameLayout have 0 height? 不可能是我的问题,因为应该已经绘制了 UI(没有在 onCreate 中调用它) 它不可能是这个Get ImageView width and height,因为我没有在 ImageView 的构造函数中调用高度和宽度 而不是这个Inflated ImageView to put in GalleryView isn't the right size,因为我确保指定了父母。
有谁知道为什么高度和宽度返回 0?我可以在我的 xml 文件中硬编码高度和宽度,但我认为这是不好的风格。
【问题讨论】:
-
我想这就是你要找的东西:stackoverflow.com/questions/5820287/…
-
哦,谢谢!!!。不敢相信我没有找到那个
-
getView后视图会被绘制吗?
标签: java android android-layout user-interface imageview