android - 嵌套的 LinearLayout 拉伸
问题描述
考虑这种布局:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/red"
android:background="@color/Red"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Red" />
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
一直水平拉伸到屏幕边缘,但高度受TextView
字符串“Red”限制,位于屏幕顶部。为什么呢?为什么TextView
只有一维拉伸?
当我切换LinearLayout
s 的方向时,效果是相反的:TextView
从上到下拉伸,但它的宽度受字符串“Red”的限制,并与屏幕左侧对齐。
解决方案
android:layout_weight="1"
从中取出TextView
并检查。
注意:如果您正在使用,ConstraintLayout
则无需服用LinearLayout
. 一切都可以由ConstraintLayout
. 您只需要设置适当的视图约束。
推荐阅读
- javascript - 如何从两步的文件中导入上下文
- angular - DSpace 7.0 在哪里放置自定义 CSS 和 Javascript?
- kubernetes - configmaps are not passing properly to the containers
- python - 使用 BeautifulSoup 从评论中提取表格数据
- spring-boot - Spring Boot webapp 启动失败
- angular - 如何使用消息 ID 按主题从 FCM 获取成功传递计数
- postgresql - 如何获取当前查询的 postgresql 错误代码
- shell - 使用 curl 在变量中执行 SQL 命令
- python - TypeError:必须是实数,而不是复数上的 str
- powershell - 在 PS-Session 中将输出写入文件