将 child 的宽度调整为 parent 的宽度
Adapt child's width to parent's width
我遇到了 xml 布局问题。
这是完整的 xml
:
<FrameLayout
android:id="@+id/root"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
tools:src="@drawable/segment_cultura"/>
<LinearLayout
android:id="@+id/message_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/black_divider"
android:padding="4dp">
<TextView
android:id="@+id/message"
style="@style/TextAppearance.AppCompat.Caption"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="2"
android:textColor="@color/white_secondary_text"
/>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="end"
android:src="@drawable/ic_message"/>
</LinearLayout>
<ImageView
android:id="@+id/cancel"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="top|end"
android:layout_margin="4dp"
android:src="@drawable/ic_close"/>
</FrameLayout>
目标:将其用作固定高度水平 RecyclerView
中的项目,仅使用 image
来确定项目的大小。其他视图,例如 message_container
或 cancel
将需要适应此大小。
问题:message_container
需要填充item的宽度,但是在[=中有长文本时不应该修改item的宽度19=]。它应该转到第二行,然后被省略。相反发生的是 message
永远不会进入第二行并使 parents(message_container
和 root
)放大以适合其文本。
我正在寻找一个只涉及 xml
的解决方案,如果我找不到它,custom view
比 adapter
.[=26 中的某些逻辑更受欢迎=]
感谢您的宝贵时间。
我使用以下方法解决了这个问题:
image.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (root != null && image.getWidth() != 0)
root.getLayoutParams().width = image.getWidth();
});
我遇到了 xml 布局问题。
这是完整的 xml
:
<FrameLayout
android:id="@+id/root"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
tools:src="@drawable/segment_cultura"/>
<LinearLayout
android:id="@+id/message_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/black_divider"
android:padding="4dp">
<TextView
android:id="@+id/message"
style="@style/TextAppearance.AppCompat.Caption"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="2"
android:textColor="@color/white_secondary_text"
/>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="end"
android:src="@drawable/ic_message"/>
</LinearLayout>
<ImageView
android:id="@+id/cancel"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="top|end"
android:layout_margin="4dp"
android:src="@drawable/ic_close"/>
</FrameLayout>
目标:将其用作固定高度水平 RecyclerView
中的项目,仅使用 image
来确定项目的大小。其他视图,例如 message_container
或 cancel
将需要适应此大小。
问题:message_container
需要填充item的宽度,但是在[=中有长文本时不应该修改item的宽度19=]。它应该转到第二行,然后被省略。相反发生的是 message
永远不会进入第二行并使 parents(message_container
和 root
)放大以适合其文本。
我正在寻找一个只涉及 xml
的解决方案,如果我找不到它,custom view
比 adapter
.[=26 中的某些逻辑更受欢迎=]
感谢您的宝贵时间。
我使用以下方法解决了这个问题:
image.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (root != null && image.getWidth() != 0)
root.getLayoutParams().width = image.getWidth();
});