在FrameLayout中裁剪ImageView使用wrap_content height
问题描述:
我希望在FrameLayout中的TextView背后有Image。我需要FrameLayout来将其高度调整到TextView里面,而不会受图像大小的影响。图像应该位于原始大小的文本后面,由FrameLayout裁剪。在FrameLayout中裁剪ImageView使用wrap_content height
这就是我试过的。
<FrameLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:layout_gravity="right|center_vertical"
android:gravity="center"
android:alpha="0.1"
android:id="@+id/img_logo"/>
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:textAllCaps="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:gravity="left|center_vertical"
android:id="@+id/text_long" />
</FrameLayout>
这几乎是我想要的,除了FrameLayout的高度受图像大小的影响。它只能随文本更改。
答
其实我认为你可以做到这一点,而不必重写任何布局或编写任何Java代码。相反,你可以将整个东西包装在一个RelativeLayout
中,在FrameLayout
之后移动TextView
,并将FrameLayout
的顶部和底部设置为与textView对齐。我承认这有点乱。为了解决这个问题,我建议将其定制为自定义视图或将其放入另一个包含的xml文件中。
编辑:实际上,您不需要FrameLayout,而是可以在ImageView上设置android:layout_alignTop和android:layout_alignBottom属性。这是它的样子:
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignTop="@+id/text_long"
android:layout_alignBottom="@id/text_long"
android:scaleType="centerCrop"
android:layout_gravity="right|center_vertical"
android:gravity="center"
android:alpha="0.1"
android:id="@+id/img_logo"/>
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:textAllCaps="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:gravity="left|center_vertical"
android:id="@id/text_long" />
</RelativeLayout>
这是一个棘手的问题要解决!我最初的反应是让FrameAlayout具有固定的高度,而ImageView具有wrap_content的高度,然后动态调整FrameLayout的大小以匹配TextView的高度。这个解决方案看起来像一个更优雅的方法(没有代码,但我可以给它一个答案的形式,如果你想看到它):http://stackoverflow.com/a/21581103/6526330 –
@ Dr.Nitpick我试过并且它不能正常工作。如果你能做到这一点,我会很乐意接受你的答案。 –
当然,我今晚试试看,并让你知道 –