根据文本高度调整图像大小

时间:2017-12-17 20:58:47

标签: android

我需要在一行上显示图像和文字。图像应根据文本高度调整大小。我知道它可以通过编程方式实现,但是也可以只用XML做到这一点吗?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView android:id="@+id/Image" ... />
    <TextView
        android:id="@+id/Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/Image"
        tools:layout_editor_absoluteX="47dp"
        tools:layout_editor_absoluteY="37dp" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

只需将视图包装在调整为textview的父视图中,然后根据父视图调整imageview的大小。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
 <!-- LOOK HERE-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

  <TextView
    android:id="@+id/Text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/Image"
    tools:layout_editor_absoluteX="47dp"
    tools:layout_editor_absoluteY="37dp" />

  <ImageView android:id="@+id/Image" 
    android:layout_heigth="match_parent"
    android:layout_width="wrap_content" />

  </LinearLayout>
</RelativeLayout>

我没有测试它,但它应该是非常接近这个的东西。

编辑:使用水平LinearLayout可以快速查看工作方式以及使用toRightOf,toLeftOf等排列的线性布局。