Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.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
Java 使用setCompoundDrawable()排列图像_Java_Android_Android Drawable - Fatal编程技术网

Java 使用setCompoundDrawable()排列图像

Java 使用setCompoundDrawable()排列图像,java,android,android-drawable,Java,Android,Android Drawable,我刚刚学习了TextView.setCompoundDrawables(),我正在列表视图中使用它将图像放置在文本的左侧。问题是缩略图的大小不同,因此文本到处都是。有没有一种简单的方法可以让文本在左手边很好地对齐 代码: TextView machineText = (TextView) v .findViewById(android.R.id.text1); machineText.setCompoundDrawablesWith

我刚刚学习了
TextView.setCompoundDrawables()
,我正在列表视图中使用它将图像放置在文本的左侧。问题是缩略图的大小不同,因此文本到处都是。有没有一种简单的方法可以让文本在左手边很好地对齐

代码:

        TextView machineText = (TextView) v
                .findViewById(android.R.id.text1);
        machineText.setCompoundDrawablesWithIntrinsicBounds(thumbnail,
                null, null, null);
屏幕截图:

        TextView machineText = (TextView) v
                .findViewById(android.R.id.text1);
        machineText.setCompoundDrawablesWithIntrinsicBounds(thumbnail,
                null, null, null);

您可以使用setCompoundDrawablePadding(int-pad)来动态更改可绘制内容和文本之间的填充。但您真正应该做的是编写自己的ListAdapter,并在自定义listitem布局中布局每个图像和文本。那就容易多了。我的经验是,位图复合绘图在不同的设备上看起来可能会有很大的不同。

试试这个:

TextView machineText = (TextView) v.findViewById(android.R.id.text1);
thumbnail.setBounds(60, 60, 60, 60);
machineText.setCompoundDrawables(bd, null, null, null);

不是60,而是根据需要设置边界。

Imageview/ImageButton是我所熟悉的唯一可以提供缩放控制的视图

如果您将图片放在ImageView中,将文本视图放在右边,您将通过更多的控制获得相同的效果。然后可以缩放图像。ScaleType=fitxy将确保图像绘制到两个xy边框。不利的一面是,如果图像不自然地适合,它们可能会出现拉伸。但是你也可以玩其他鳞片

    <ImageView
        android:id="@+id/my_iv"
        android:layout_width="30dp"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/overflow"

     />

    <TextView 
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_toRightOf="@id/my_iv"
        />


我没有使用TextView+drawable选项,但您也可以尝试确保图像大小相同。我不确定android是如何选择可绘制尺寸的,当它是textview的一部分时,因此,我不确定这是否有效。

显示一些代码或您的问题的屏幕截图。@GrIsHu好了。您是在动态膨胀图像吗?我建议您在
ImageView
中设置图像,除了将其设置到
TextView
中,因为您无法管理您在TextView中设置的图像的高度和宽度。@GrIsHu谢谢你的建议,或者我可以只做
缩略图。setBounds(…)