使用wrap_content高度在FrameLayout中裁剪ImageView

时间:2016-07-04 16:31:15

标签: android

我想在FrameLayout中将Text放在TextView后面。我需要FrameLayout来调整它的高度到TextView里面而不受图像大小的影响。图像应该在文本的原始尺寸后面,由FrameLayout裁剪。

这就是我的尝试。

<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的高度受图像大小的影响。它应该只用文字改变。

1 个答案:

答案 0 :(得分:2)

实际上我认为你可以在不覆盖任何布局或编写任何java代码的情况下做到这一点。相反,您可以将RelativeLayout移动TextView移动到FrameLayout之后的FrameLayout并将<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> 的顶部和底部设置为与textView对齐。我承认它有点乱;为了解决这个问题,我建议将其设置为自定义视图或将其放入您包含的另一个xml文件中。

编辑:实际上,你不再需要FrameLayout,而是可以在ImageView上设置android:layout_alignTop和android:layout_alignBottom属性。这是它的样子:

foreach (TableCell cell in e.Row.Cells)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string text = cell.Text.Trim();
        cell.BackColor = (String.IsNullOrEmpty(text) || text == "&nbsp;") ? Color.Red : Color.White;
    }
}
相关问题