Android 如何防止ImageView调整其父视图的大小?

Android 如何防止ImageView调整其父视图的大小?,android,android-layout,resize,imageview,Android,Android Layout,Resize,Imageview,注意:我已经检查过了,但是没有一个答案有用。我重复这个问题是为了说明我的具体问题和需要 我在android应用程序中创建了一个自定义视图,显示即将到来的活动的数据(标题、位置、价格、说明等)。除此数据外,此视图上还显示图标和封面照片(图标显示在视图左上角的ImageView中,封面修改为alpha为128,然后显示在视图内容后面。这是我目前的布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro

注意:我已经检查过了,但是没有一个答案有用。我重复这个问题是为了说明我的具体问题和需要

我在android应用程序中创建了一个自定义视图,显示即将到来的活动的数据(标题、位置、价格、说明等)。除此数据外,此视图上还显示图标和封面照片(图标显示在视图左上角的
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>
这一切都是可行的,但它调整了视图的大小,为封面照片腾出了空间,这也是我想要避免的。我在设置可绘制性之前尝试使用
getHeight()
,然后设置可绘制性,并使用
bkg.getLayoutParams().height=height
设置封面的
ImageView
高度,但调用
getHeight()
返回
0
,因为此时视图在技术上是不可见的,所以封面照片是不可见的。视图应该是这样的,背景图像被裁剪并居中:

tl;dr/摘要: 如何防止
ImageView
调整其父视图的大小?

我发现另一个问题与我的问题非常相似:

这需要使用代码来完成。您需要在屏幕呈现几毫秒后调用这些大小API。因此,如果您在100毫秒后调用它,使用任何已呈现视图的
postDelayed
方法,您将获得大小

使用该方法,我将我的
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();
    }
}
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();
    }
}