Reputation: 3160
I'm trying to create a list view with text and images. Filling the layout works but I have a problem with layout_height="wrap_content"
of the ImageView
. After filling the List, the list items are twice as high as the image hight with a lot of space below and above the image itself. How do I avoid the empty space and wrap the ListItem
height around the image?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/product_preview_product"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center" />
<ImageView
android:id="@+id/product_preview_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/product_preview_product" />
</RelativeLayout>
The getView()
method from the modified adapter:
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.product_preview, parent, false);
}
Product product = getItem(position);
TextView productText = (TextView) convertView.findViewById(R.id.product_preview_product);
productText.setText(product.name);
// load image
ImageView imageView = (ImageView) convertView.findViewById(R.id.product_preview_image);
imageView.setImageDrawable(image);
return convertView;
}
Upvotes: 0
Views: 159
Reputation: 4574
I encountered the same Problem many times and setting
android:adjustViewBounds="true"
android:scaleType="fitCenter"
On my ImageViews did the trick, just give it a try. The Docs to adjustViewBounds says
Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.
Upvotes: 2
Reputation: 131
set the weight of the layouts and their child widgets android:weightSum="1.2"
<TextView
android:id="@+id/product_preview_product"
android:layout_width="wrap_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_weight=".2"
android:gravity="center" />
<ImageView
android:id="@+id/product_preview_image"
android:layout_width="wrap_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_below="@id/product_preview_product" />
Upvotes: 0
Reputation: 101
use this
android:layout_width="wrap_content"
android:layout_height="wrap_content"
if you image is big then set appropriate size here.
Upvotes: 0