【发布时间】:2017-04-12 01:14:50
【问题描述】:
我正在 Android 中创建一个动态添加长字符串的片段。行数是可变的。我正在使用 LinearLayout 来包含图像下方的文本。我希望文本保持图像的宽度并在下方垂直扩展。当我在 TextView 的 layout_height 上使用 wrap_content 时,它会在第二行之后剪辑文本。我可以设置 height 或 lines 属性,并且父 LinearLayout 可以正确展开,如何使用 xml 属性使其具有动态高度?
这是布局代码:
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2017, University of Colorado Boulder
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="24dp">
<LinearLayout
android:id="@+id/image_and_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="8dp">
<ImageView
android:id="@+id/image"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:onClick="launchSimulation"
android:paddingEnd="20dp"
android:scaleType="fitStart"
app:srcCompat="@drawable/logo" />
</LinearLayout>
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Super long example text. Super long example text. Super long example text. Super long example text. Super long example text. Super long example text. Super long example text. Super long example text. Super long example text. " />
</LinearLayout>
结果如下:
这是我想要的样子:
【问题讨论】: