如何防止 ImageView 调整其 parent 的大小?

How can I prevent an ImageView from resizing its parent?

Note: I have checked this question, but none of the answers helped. I'm restating the question to specify my specific problems and needs.

我在我的 android 应用程序中创建了一个自定义视图,用于显示即将发生的事件的数据(标题、位置、价格、描述等)。除了这个数据之外,这个视图上还显示了图标和封面照片(图标显示在视图 upper-left 角的 ImageView 中,封面被修改为具有 alpha 128,然后 显示在 视图内容的后面。这是我目前的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/eventview_main" android:layout_width="match_parent"
    android:layout_height="wrap_content" android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:background="@color/color_black" android:elevation="16dp">

    <!-- OBJECT IN QUESTION -->
    <ImageView
        android:id="@+id/eventview_cover"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="false"
        android:cropToPadding="false"
        android:scaleType="centerCrop" />

    <ImageView android:id="@+id/eventview_icon"
        android:layout_width="100dp" android:layout_height="100dp"
        android:layout_marginLeft="@dimen/half_margin"
        android:layout_marginRight="@dimen/half_margin"
        android:layout_marginTop="@dimen/half_margin" android:elevation="8dp" />

    <TextView android:id="@+id/eventview_title"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/eventview_price"
        android:layout_alignParentEnd="true" android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" android:layout_marginTop="@dimen/half_margin"
        android:layout_toEndOf="@+id/eventview_icon" android:layout_toRightOf="@+id/eventview_icon"
        android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/color_white" />

    <TextView android:id="@+id/eventview_location"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignParentEnd="true" android:layout_alignParentRight="true"
        android:layout_below="@+id/eventview_price" android:layout_toEndOf="@+id/eventview_icon"
        android:layout_toRightOf="@+id/eventview_icon" android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/color_white" />

    <TextView android:id="@+id/eventview_price"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignParentEnd="true" android:layout_alignParentRight="true"
        android:layout_below="@+id/eventview_title" android:layout_toEndOf="@+id/eventview_icon"
        android:layout_toRightOf="@+id/eventview_icon" android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/color_white" />

    <TextView android:id="@+id/eventview_shortdesc"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/eventview_title"
        android:layout_alignParentEnd="true" android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true" android:layout_alignStart="@+id/eventview_title"
        android:layout_below="@+id/eventview_icon"
        android:layout_marginBottom="@dimen/half_margin"
        android:layout_marginLeft="@dimen/half_margin" android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/color_white" android:width="0dip" />

</RelativeLayout>

Activity 实例化我的视图时,它必须提供数据源(自定义 class)。然后(在 setSource() 下)调用方法 updateLayout(),它设置图标图像和各种 TextView。此外,它将 eventview_cover 的来源设置为 BitmapDrawable(以修改原始照片的 alpha)。这反过来又扩展了视图的高度(我想避免)。视图的高度应仅由图标的 TextViewImageView 的高度决定。这是 EventView:

的代码
public class EventView extends RelativeLayout {

    private MergeEvent src = null;

    public MergeEvent getSource() {
        return src;
    }

    protected void setSource(MergeEvent e) {
        src = e;
        //The data was updated, so we need to update the view!
        updateLayout();
    }

    public EventView(MergeEvent src, Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setSource(src);
    }

    public EventView(MergeEvent src, Context context, AttributeSet attrs) {
        super(context, attrs);
        setSource(src);
    }

    public EventView(MergeEvent src, Context context) {
        super(context);
        setSource(src);
    }

    protected void updateLayout() {
        if (getSource() != null) {
            //Get source data.
            MergeEvent src = getSource();
            //Set the layout.
            View.inflate(getContext(), R.layout.eventview, this);
            //Get references to the TextViews and ImageViews
            TextView title = (TextView)findViewById(R.id.eventview_title),
                     price = (TextView)findViewById(R.id.eventview_price),
                     location = (TextView)findViewById(R.id.eventview_location),
                     shortdesc = (TextView)findViewById(R.id.eventview_shortdesc);
            ImageView icon = (ImageView)findViewById(R.id.eventview_icon), 
                      cover = (ImageView)findViewById(R.id.eventview_cover);
            icon.setImageBitmap(src.getIcon());
            title.setText(src.getTitle());
            price.setText("$" + src.getPrice());
            location.setText(src.getLocation());
            shortdesc.setText(src.getShortDescription());
            //Create a new BitmapDrawable from the source's cover (a Bitmap)
            BitmapDrawable bd = new BitmapDrawable(getResources(), src.getCover());
            bd.setAlpha(128);
            bkg.setImageDrawable(bd);
            requestLayout();
        }
    }

}

这一切都有效,但它会调整视图大小以为封面照片腾出空间,我想再次避免这种情况。我尝试在设置可绘制对象之前使用 getHeight(),然后设置可绘制对象,并使用 bkg.getLayoutParams().height = height 设置封面的 ImageView 高度,但是对 getHeight() 的调用返回了 0 因为此时视图在技术上是不可见的,所以封面照片是不可见的。视图应如下所示,背景图像被裁剪并居中:

tl;dr/summary:

如何防止 ImageView 调整其 parent 的大小?

我发现 this answer 在另一个与我非常相似的问题上:

This needs to be done using code. You need to call those size APIs a few milliseconds after the screen renders. So, if you call it 100 milliseconds after, using postDelayed method of any view that has been rendered, you will get the sizes.

使用它,我将 updateLayout() 方法修改为:

protected void updateLayout() {
    if (getSource() != null) {
        MergeEvent src = getSource();
        View.inflate(getContext(), R.layout.eventview, this);
        TextView title = (TextView)findViewById(R.id.eventview_title);
        TextView price = (TextView)findViewById(R.id.eventview_price);
        TextView location = (TextView)findViewById(R.id.eventview_location);
        TextView shortdesc = (TextView)findViewById(R.id.eventview_shortdesc);
        ImageView i = (ImageView)findViewById(R.id.eventview_icon);
        i.setImageBitmap(src.getIcon());
        title.setText(src.getTitle());
        price.setText("$" + src.getPrice());
        location.setText(src.getLocation());
        shortdesc.setText(src.getShortDescription());
        //use postDelay to set the ImageView's source and max size after 100 miliseconds (plenty of time for the UI to be drawn)
        this.postDelayed(new Runnable() {

            @Override
            public void run() {
                BitmapDrawable bd = new BitmapDrawable(getResources(), src.getCover());
                bd.setAlpha(128);
                ImageView bkg = (ImageView)findViewById(R.id.eventview_cover);
                bkg.setMaxHeight(getHeight());
                bkg.setImageDrawable(bd);
            }

        }, 100);
        requestLayout();
    }
}