【问题标题】:Scaling ImageView to size of a TextView in XML将 ImageView 缩放为 XML 中 TextView 的大小
【发布时间】:2014-03-04 13:44:06
【问题描述】:

我在水平线性布局中有一个 TextView 和一个 ImageView。是否可以缩放 ImageView 使其相对于布局文件中 TextView 的大小?

基本上我想要类似的东西:-

不是这样的

布局文件是

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <ImageView />
    <TextView />
</LinearLayout>

【问题讨论】:

  • ImageView 高度:matchparent,TextView 高度:wrapcontent,容器 LinearLayout 高度:包装内容 ...也许
  • 尝试使用图片作为textview的leftdrawable。

标签: android layout scaling


【解决方案1】:

是的,您可以在相对于TextView 的高度缩小ImageView。要匹配TextView 的高度,请将ImageViewmatch_parent 设置为android:layout_height,这将有助于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" />

【讨论】:

  • 设置一个左复合drawable不是更有效吗?
  • 有可能...但他问是否可以缩放 ImageView 使其与布局文件中 TextView 的大小相关? 请,正确阅读问题。
  • 你基本上使用了我在评论中写的内容:p
  • @DarkoRodic...我什至没有注意到你的评论 ;)
  • @HamidShatu 我正确阅读了这个问题并解释了他们想要实现的目标。具有沉重的视图层次结构对性能不利。当复合可绘制更有效时,为什么要使用线性布局而不是复合可绘制?
【解决方案2】:

试试这个

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

【讨论】:

    【解决方案3】:

    通过使用它,您可以简单地在文本视图中添加图像

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      相关资源
      最近更新 更多