如何为ImageView提供基于百分比的最小宽度?

时间:2014-09-16 17:13:34

标签: android xml layout imageview alignment

我有一个列表视图,我用75x75像素图像和一些文本填充:

<?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="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>

我现在正在使用可以检索任意大小图像的图像服务器(我要求的图像占设备宽度的30%),所以我想让我的ImageView始终采取高达列表视图宽度的30%。我尝试对android:layout_weight=".3"android:layout_weight=".7"使用ImageViewTextView,但这会导致两个问题:

  • 由于我的图像是异步加载的,因此文本在加载图像时会向右移动,而不是已经在正确的位置。之前这不是问题,因为我可以使用android:minHeightandroid:minWidth
  • 文字和图片不再很好地对齐(有些是向左或向右的,设置gravitylayout_gravity没有帮助。)

那么,如何使ImageView占据宽度的30%(甚至在设置图像之前),并使所有图像和文本正确对齐?

0 个答案:

没有答案