首页 > 解决方案 > 带有百分比指南的约束布局在 wrap_content 中不起作用

问题描述

大家好,我想要一个屏幕高度为 30% 的图像视图,我该怎么做?这是我的代码,但是当我更改约束布局高度以匹配父级时它不起作用,当我将它设置为 wrap_content 现在我在屏幕上什么都没有这是我的代码:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.AppCompatImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline1"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/pic" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.25"/>
</android.support.constraint.ConstraintLayout>

当我设置高度以匹配父级时,我想在recyclerview上使用它,这就是在此处输入图像描述它下面有很多空白空间的方式

更新

标签: androidandroid-layoutandroid-constraintlayout

解决方案


我假设您提供的 XML 是针对您中的每个项目的,RecyclerView并且您希望每个项目占据RecyclerView. 一个RecyclerView项目是在onCreateViewHolder()适配器中创建的,通常只考虑它自己的内容并膨胀到容纳其内容所需的大小。

您想强加一个外部要求,即该RecyclerView项目必须占据 1/3 的高度RecyclerView。在 XML 中没有办法做到这一点,因此您将不得不求助于代码。

我认为这很容易,但它有点涉及,但请耐心等待。

您需要确定 的高度,RecyclerView以便计算该值的 1/3 作为每个项目的高度。RecyclerView不幸的是,正如我所发现的,直到创建视图持有者才确定高度。所以,我们陷入了困境:我们需要 的高度RecyclerView来构建视图持有者,但必须构建视图持有者来确定RecyclerView.

为了解决这个问题,我们将设置高度RecyclerViewmatch_parent。这将使它与RecyclerView其父级一样高。RecyclerView如果完全测量之前的父级,我们可以获得高度。我们将使用一个全局布局监听器来捕获父级的高度。此代码应在创建RecyclerView(here mRecyclerView) 之后和设置适配器之前执行。在我的测试套件中,我定义onCreate()了活动的离子。

    mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // Remove the listener so we don't get called again.
            mRecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            // Capture the height of the parent view group.
            int height = ((ViewGroup) mRecyclerView.getParent()).getHeight();
            // And let the adapter know the height.
            adapter.setItemHeight(height);
            // Now that we know the height of the RecyclerView and its items, we
            // can set the adapter so the items can be created with the proper height.
            mRecyclerView.setAdapter(adapter);
        }
    });

添加setItemHeight()到适配器以捕获项目高度。

private int mItemHeight;

public void setItemHeight(int parentHeight) {
    mItemHeight = parentHeight / 3;
}

最后,我们可以使用项目高度来创建项目:

@Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
    view.getLayoutParams().height = mItemHeight;
    return new ItemHolder(view);
}

RecyclerView如果有填充、边距或装饰,您可能需要调整项目的高度。


推荐阅读