Android 设置为匹配父视图时,视图宽度为零

Android 设置为匹配父视图时,视图宽度为零,android,imageview,Android,Imageview,我有一个ImageView在LinearLayout的内部,它是RecyclerView的一个项目。我将所有这些视图的宽度设置为MATCH\u PARENT 现在,我尝试在加载图像之前使用纵横比计算高度,这样就不会调整大小 但是,我看到的是,在加载图像之前,它的宽度是零 问题是,我在应用程序的其他部分也做了类似的事情,它工作得很好,我真的不知道我在这里遗漏了什么 回收查看项目: <?xml version="1.0" encoding="utf-8"?> <LinearLayo

我有一个
ImageView
LinearLayout
的内部,它是
RecyclerView
的一个项目。我将所有这些视图的宽度设置为
MATCH\u PARENT

现在,我尝试在加载图像之前使用纵横比计算高度,这样就不会调整大小

但是,我看到的是,在加载图像之前,它的宽度是零

问题是,我在应用程序的其他部分也做了类似的事情,它工作得很好,我真的不知道我在这里遗漏了什么

回收查看项目:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white">

    <LinearLayout

        ...

    </LinearLayout>

    <!-- This is the image -->
    <ImageView
        android:id="@+id/descubre_shop_banner"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="16dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorLightText"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginBottom="8dp"/>

</LinearLayout>
我试图将
ImageView
的宽度设置为
MATCH\u parent
,但没有成功。有什么线索吗


关于,在完全创建视图之前,视图尺寸始终为0,如果您想知道实际宽度,必须使用以下方法:
post()

也就是说,你实际上应该使用另一种方法来保持这个比率,而不是自己实际计算它!
有关详细信息,请参阅此线程。

在完全创建视图之前,视图尺寸始终为0。如果您想知道实际宽度,必须使用以下方法:
post()

也就是说,你实际上应该使用另一种方法来保持这个比率,而不是自己实际计算它!
有关更多信息,请参阅此线程

此方法在绑定视图时调用,因此它已创建此方法在绑定视图时调用,因此它已创建
mBannerTarget = new Target()
{
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from)
    {
        mShopBannerImageView.setImageBitmap(bitmap);

        // Wrap content when the images is loaded.
        mShopBannerImageView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
        mShopBannerImageView.setBackgroundColor(-1);
        mShopBannerImageView.setAlpha(1.0f);

        Animation fadeOut = new AlphaAnimation(0, 1);
        fadeOut.setInterpolator(new AccelerateInterpolator());
        fadeOut.setDuration(250);
        mShopBannerImageView.startAnimation(fadeOut);

        LOADED = true;
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable)
    {
        mShopBannerImageView.setBackgroundColor(
                mContext.getResources().getColor(android.R.color.holo_red_dark));

        mShopBannerImageView.setAlpha(0.2f);
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable)
    {
        // This is 0
        Log.d(Properties.TAG, "" + mShopBannerImageView.getWidth());

        // Remove the old bitmap
        mShopBannerImageView.setImageBitmap(null);

        // Set the height using the width and the aspect ratio.
        mShopBannerImageView.getLayoutParams().height =
                (int) (mShopBannerImageView.getWidth() * shop.getAspectRatio());

        // Set a color while loading.
        mShopBannerImageView.setBackgroundColor(
                mContext.getResources().getColor(R.color.colorAccent));
        mShopBannerImageView.setAlpha(0.25f);
    }
};
view.post(new Runnable() {
            @Override
            public void run() {
                //   This is where width would be different than 0
            }
        });