How can I give my ImageView a percentage-based minimum width?



I have a list view, which I was populating with 75x75 px images, and some text:



<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingLeft="0dp"
android:paddingBottom="10dp"
android:minHeight="90dp"
android:minWidth="90dp" />

<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textSize="20sp"
android:textIsSelectable="false"
android:layout_gravity="center_vertical" />

</LinearLayout>


I am now using an image server that can retrieve images of any size (I am requesting images that are 30% of the devices's width), so I would like to make my ImageView always take up 30% of the width of the list view. I tried using android:layout_weight=".3", and android:layout_weight=".7" for the ImageView and TextView, but this caused two problems:



  • Since my images are loaded asynchronously, the text moves to the right when the image is loaded, instead of already being in the correct place. This wasn't a problem before, since I could use android:minHeight and android:minWidth.

  • The text and images are no longer nicely aligned (some are further to the left or right, and setting gravity and layout_gravity did not help.)


So, how can I make the ImageView take up 30% of the width (even before an image is set), and have all the images and text aligned properly?


No comments:

Post a Comment