在XML中将ImageView缩放到与TextView相同的大小

9
我有一个TextView和一个ImageView在一个水平线性布局中。是否可以缩放ImageView,使其相对于布局文件中TextView的大小而定呢?
基本上我想要类似于这样的效果: enter image description here 而不是像这样: enter image description here 布局文件将会是:
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <ImageView />
    <TextView />
</LinearLayout>

ImageView高度:matchparent,TextView高度:wrapcontent,容器LinearLayout高度:wrap content ...也许。 - Darko Rodic
3
尝试将图像用作TextView的leftdrawable。 - Brajesh Kumar
3个回答

9

是的,您可以将ImageView的高度按比例缩小以匹配TextView的高度。要使ImageViewTextView的高度相同,请在ImageViewandroid:layout_height中设置match_parent,这将帮助ImageView占据TextView的高度。请尝试以下操作...

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="30sp" />
</LinearLayout>

您可以通过以下方式实现相同的结果...
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/ic_launcher"
    android:text="TextView"
    android:gravity="center_vertical"
    android:textSize="30sp" />

1
可能可以...但他正在问是否可以按照布局文件中TextView的大小来缩放ImageView?请仔细阅读问题。 - Hamid Shatu
你基本上使用了我在评论中写的内容 :p - Darko Rodic
@DarkoRodic...我甚至没有注意到你的评论 ;) - Hamid Shatu
@BenPearson...是的,我同意你的观点。个人而言,我会使用compound drawable(复合绘制)。 - Hamid Shatu
@BenPearson...我已经根据你的论点更新了我的答案。谢谢你的关注。 :) - Hamid Shatu
显示剩余4条评论

-1

试试这个

 <TextView
        android:id="@+id/Text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:drawableLeft="@drawable/image"
        />

-1

通过使用此功能,您可以简单地在文本视图中添加图片

<TextView>
android:drawableLeft="@drawable/picture.png"
</TextView>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接