我有以下代码片段:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/collected_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/card_text_color"
android:textSize="@dimen/card_headline_textsize"
android:layout_marginBottom="@dimen/card_headline_marginBottom"
android:text="Foo" />
<ImageView
android:id="@+id/collected_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="140dp"
android:background="@android:color/holo_red_dark"
android:adjustViewBounds="true"
android:layout_below="@id/collected_item_title"
android:layout_marginEnd="8dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/collected_item_image"
android:layout_below="@id/collected_item_title"
android:text="Foo"
android:textColor="@color/card_text_color" />
</RelativeLayout>
我喜欢左边的ImageView和第二个TextView,或者在图像的右边,或者取决于图像的大小。图像应该增长,直到达到最大高度140dp。
如果图像的宽度太宽,我该如何自动换行?不幸的是,我现在不知道该怎么做。是的我可以通过编程方式完成,但我喜欢只使用xml达到目标。
如果使用其他布局可以执行此行为,则包装RelativeLayout不必是RelativeLayout。
听起来你需要一个FlowLayout
。 Android没有提供开箱即用的功能,但请查看此答案。 How can I do something like a FlowLayout in Android?
感谢chessdork的回答,我找到了google的flexbox布局。它由以下git存储库提供。 https://github.com/google/flexbox-layout