【发布时间】:2018-10-09 16:41:39
【问题描述】:
Imageview来自网络图片,他的高度随宽度缩放
图像视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_intro_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/product_top"
android:divider="@null"
android:adjustViewBounds="true"/>
</LinearLayout>
列表视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:background="#FFFFFF">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:id="@+id/lv_intro"
android:divider="@null"></ListView>
</LinearLayout>
设置ListView高度
public static void setListViewHeightBasedOnChildren(ListView listView, int basicsHeight,int plusItem) {
int totalHeight = basicsHeight;
ListAdapter adapter = listView.getAdapter();
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, listView);
listItem.measure(0, 0);
int itemheight=listItem.getMeasuredHeight();
Log.e("ItemView"+i, "height" + itemheight);
totalHeight += itemheight + plusItem;
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
listView.setLayoutParams(params);
}
但是这个日志一直打印高度为0,
又想了想,加个ImageView监听事件,还是什么都得不到 我仍然无法获得 ImageView 的高度
@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.product_item_intro, null);
iv_intro_img = convertView.findViewById(R.id.iv_intro_img);
}
String imgurl = datas.get(position); //+ "_500x500.jpg";
RequestOptions myOptions = new RequestOptions()
.fitCenter();
Glide.with(mContext).load(imgurl).apply(myOptions).into(iv_intro_img);
iv_intro_img.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int itemtHeight = iv_intro_img.getHeight();
int itemtHeight2= iv_intro_img.getMeasuredHeight();
Log.e("itemimageview" + position, "height" + itemtHeight + "," + itemtHeight2);
iv_intro_img.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
return convertView;
}
但还没有。希望大家帮我设置ListView的高度。
【问题讨论】:
标签: android listview imageview height