android - 带有百分比指南的约束布局在 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>
解决方案
我假设您提供的 XML 是针对您中的每个项目的,RecyclerView
并且您希望每个项目占据RecyclerView
. 一个RecyclerView
项目是在onCreateViewHolder()
适配器中创建的,通常只考虑它自己的内容并膨胀到容纳其内容所需的大小。
您想强加一个外部要求,即该RecyclerView
项目必须占据 1/3 的高度RecyclerView
。在 XML 中没有办法做到这一点,因此您将不得不求助于代码。
我认为这很容易,但它有点涉及,但请耐心等待。
您需要确定 的高度,RecyclerView
以便计算该值的 1/3 作为每个项目的高度。RecyclerView
不幸的是,正如我所发现的,直到创建视图持有者才确定高度。所以,我们陷入了困境:我们需要 的高度RecyclerView
来构建视图持有者,但必须构建视图持有者来确定RecyclerView
.
为了解决这个问题,我们将设置高度RecyclerView
为match_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
如果有填充、边距或装饰,您可能需要调整项目的高度。
推荐阅读
- c++ - 如何从字符串数组转换为字符数组数组?
- amazon-ec2 - 如何将社区版 sonarqube aws 实例替换为开发者版?
- swift - 如何在swift中选择aTableView部分中的单元格
- python - 站点包文件夹存在于 python2 但不存在于 python3
- shopify - Shopify 产品/更新 webhook 在库存更改时触发
- android - ConstraintSet.connect() 中的边距参数是 px 还是 dp?
- asp.net-core - 将启用 OData 的 api 控制器转换为将输出 json 格式为 TitleCase 而不是驼峰式大小写
- graphics2d - 需要合并 2 个 eps 文件以在 Java 中生成 output.eps
- php - 从 PHP 脚本在 odoo 中制作 POS
- javascript - 使用 Pulumi 创建简单的 Azure HDInsight Spark 集群时出错