Android 带有百分比准则的约束布局在包装内容中不起作用
嗨,伙计们,我想要一个屏幕高度为30%的图像视图,我该怎么做?这是我的代码,但当我更改constraintLayout高度以匹配父级时,它不起作用。它占用所有屏幕,当我将其设置为wrap_内容时,现在屏幕中没有任何内容 这是我的代码:Android 带有百分比准则的约束布局在包装内容中不起作用,android,android-layout,android-constraintlayout,Android,Android Layout,Android Constraintlayout,嗨,伙计们,我想要一个屏幕高度为30%的图像视图,我该怎么做?这是我的代码,但当我更改constraintLayout高度以匹配父级时,它不起作用。它占用所有屏幕,当我将其设置为wrap_内容时,现在屏幕中没有任何内容 这是我的代码: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.andro
<?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>
下面有很多空地
我假设您提供的XML是针对RecyclerView
中的每个项目的,并且您希望每个项目占据RecyclerView
高度的1/3。在适配器的onCreateViewHolder()
中创建一个RecyclerView
项,它通常只考虑其自身的内容,并膨胀到容纳其内容所需的大小
您希望强加一个外部要求,即RecyclerView
项必须占据RecyclerView
高度的1/3。在XML中无法做到这一点,因此您必须求助于代码
我原以为这很容易,但有点牵扯其中,但请容忍我
您需要确定回收视图的高度,以便计算该值的1/3作为每个项目的高度。不幸的是,正如我所发现的,在创建视图保持架之前,RecyclerView
的高度是不确定的。因此,我们陷入了困境:我们需要RecyclerView
的高度来构建视图保持架,但视图保持架必须被构建来确定RecyclerView
的高度
为了解决这个问题,我们将RecyclerView
的高度设置为match\u parent
。这将使RecyclerView
与其父级一样高。如果对RecyclerView
之前的父对象进行了完全测量,则可以得到高度。我们将使用全局布局侦听器来捕获父级的高度。此代码应在创建RecyclerView
(此处mRecyclerView
)之后和设置适配器之前执行。在我的测试套件中,我已将其定义为活动的一部分
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
有填充、边距或装饰,您可能需要调整项目的高度。检查@Osairon它不起作用我试试看你能在2分钟内再次检查我的答案吗?我希望RecyclerView中的所有项目占据屏幕的30%@Cheticamp@razaAmini还不清楚。我在您展示的XML中没有看到RecyclerView
。那是什么XML?它是用于RecyclerView
中项目的XML还是其他内容?你想在你的RecyclerView
屏幕上正好放三个项目吗?非常感谢你的帮助,我需要30%的屏幕来显示我的视图高度,是的,这个图像视图是我的RecyclerView孩子我的RecyclerView是一个简单的匹配项\u家长的高度和布局中的wight RecyclerView我能看一下我的更新图像吗请看我试图建立这样一个视图:recyclerview->first%30是一个图像视图,其余的项目也是recyclerview,但是使用水平线性布局管理器,我真的需要帮助,非常感谢我需要一个占屏幕30%的布局或视图