【问题标题】:constraint layout with percentage Guideline dont work in wrap_content带有百分比指南的约束布局在 wrap_content 中不起作用
【发布时间】:2018-05-11 04:07:21
【问题描述】:

大家好,我想要一个屏幕高度为 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 上使用它,这就是如何变成 它下面有很多空白空间

【问题讨论】:

标签: android android-layout android-constraintlayout


【解决方案1】:

我假设您呈现的 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)之后和设置适配器之前执行。在我的测试套件中,我定义了 ion 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 有内边距、边距或装饰,您可能需要调整项目的高度。

【讨论】:

  • 嗨,您能在 2 分钟后再次检查我的答案吗?
  • 我希望我在 recyclerView 中的所有项目占据屏幕的 30% @Cheticamp
  • @razaAmini 仍然不清楚。我在您提供的 XML 中没有看到 RecyclerView。那个 XML 是什么?是 RecyclerView 中的项目的 XML 还是其他东西?您是否试图在您的 RecyclerView 中恰好在屏幕上显示三个项目?
  • 非常感谢帮助我,我的视图高度正好需要 30% 的屏幕,是的,这个图像视图是我的 recyclerview 孩子我的 recyclerview 是布局中的简单 match_parent 高度和重量 recyclerview
  • 嗨,你能看我的更新图片吗?请看我试图构建这样的视图:recyclerview -> 第一个 %30 是一个 imageview,其余的项目也是 recyclerview,但是我有一个水平的线性布局管理器真的需要帮助非常感谢我需要一个占屏幕 30% 的布局或视图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-09
  • 2020-10-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多