Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何为ImageView提供基于百分比的最小宽度?_Android_Xml_Layout_Imageview_Alignment - Fatal编程技术网

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

Android 如何为ImageView提供基于百分比的最小宽度?,android,xml,layout,imageview,alignment,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:orie

我有一个列表视图,其中填充了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\u weight=“.3”
,和
android:layout\u weight=“.7”
来查看
ImageView
TextView
,但这导致了两个问题:

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

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

如果在从服务器请求图像之前加载带有默认位图的图像视图,可以避免第一个问题。您可以创建一个所需的位图,该位图具有正确的尺寸。至于你的第二个问题,是接收到的图像宽度不同造成的吗?@greenapps谢谢,我会尝试创建一个位图占位符。所有接收到的图像都具有相同的宽度和高度(尺寸仅随设备的不同而变化)。