具有百分比准则的约束布局在 wrap_content 中不起作用

constraint layout with percentage Guideline dont work in wrap_content

大家好,我想要一个高度为屏幕 30% 的图像视图,我应该怎么做?这是我的代码,但是当我更改 constraintLayout 高度以匹配父级时它不起作用,它占据了所有屏幕,当我将它设置为 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 上使用它,当我设置高度以匹配父级时,这就是如何变成 它下面有很多空 space

我假设您呈现的 XML 是针对您 RecyclerView 中的每个项目的,并且您希望每个项目占据 RecyclerView 高度的 1/3 ]. RecyclerView 项目是在适配器的 onCreateViewHolder() 中创建的,通常只考虑其自身的内容并膨胀到容纳其内容所需的大小。

您想施加一个外部要求,即 RecyclerView 项目必须占据 RecyclerView 高度的 1/3。在 XML 中无法执行此操作,因此您将不得不求助于代码。

我以为这很容易,但有点复杂,请耐心等待。

您将需要确定 RecyclerView 的高度,以便计算该值的 1/3 作为每个项目的高度。不幸的是,正如我所发现的, RecyclerView 的高度在创建视图持有者之前是不确定的。所以,我们进退两难:我们需要 RecyclerView 的高度来构建视图持有者,但必须构建视图持有者来确定 RecyclerView.

的高度

为了解决这个问题,我们将 RecyclerView 的高度设置为 match_parent。这将使 RecyclerView 与其父元素一样高。如果 RecyclerView 之前的父级完全测量,我们可以得到高度。我们将使用一个全局布局侦听器来捕获父级的高度。此代码应在创建 RecyclerView(此处为 mRecyclerView)之后和设置适配器之前执行。在我的测试套件中,我定义了 activity.

的离子 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 有填充、边距或装饰,您可能需要调整项目的高度。