android - 按百分比的 ConstraintLayout 是重叠的
问题描述
所以我在学习android编程。我正在尝试制作一个可以读取所有文件并将其显示在列表中的应用程序。所以我正在制作列表项布局,起初我使用使用权重的线性布局,但 android studio 建议嵌套权重不好。因此,经过一番搜索,我了解到更好的选择是约束布局。所以我尝试使用约束布局来制作它
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="80dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="4dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/thumbnail_imageview"
android:layout_width="80dp"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
<TextView
android:id="@+id/filename_textview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.5"
android:gravity="center_vertical"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:text="File Name"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/thumbnail_imageview"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/filesize_textview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.5"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5"
android:gravity="center_vertical"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:text="File Size"
app:layout_constraintTop_toBottomOf="@id/filename_textview"
app:layout_constraintLeft_toRightOf="@+id/thumbnail_imageview"/>
<TextView
android:id="@+id/filetype_textview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.5"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5"
android:gravity="center_vertical"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:text="File Type"
app:layout_constraintTop_toBottomOf="@id/filename_textview"
app:layout_constraintLeft_toRightOf="@+id/filesize_textview"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
问题是文件大小和文件类型各占 50%,但它们是重叠和溢出的。我无法理解我做错了什么。
解决方案
这是因为layout_constraintWidth_percent
(以及高度)的值基于父容器的宽度 - 但是您的项目左侧是thumbnail_imageview
占用空间的缩略图。因此,两个宽度为 50% 的小部件分别剩下不到 100% 的水平空间。
要修复它,请将您的两个项目包装在一个容器(例如另一个ConstraintLayout
)中,并将该容器与缩略图的右侧对齐。
推荐阅读
- android - 为什么在使用 Cordova 构建 APK 时出现此错误?
- c - NCurses 标准输入重定向
- php - 在codeigniter中向控制器发送数据视图
- c# - 使用 URL 作为参数的 Web API 路由在包含时间段时不再有效
- javascript - 如何在量角器中获取特定路径中的多个 .dat 文件
- reporting-services - SSRS 报告多重连接
- python - Python + Azure 存储队列 receive_messages()
- javascript - 使用 JSX 元素添加 `title` 属性换行符
- c++ - 两次调用 toString 函数后出现分段错误
- python - 使用python循环股市API