嵌套的 LinearLayout 拉伸
Nested LinearLayout stretch
考虑这个布局:
<?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" 和与屏幕左侧对齐。
从 TextView
中删除 android:layout_weight="1"
并检查。
注意:如果您使用的是ConstraintLayout
,则无需使用LinearLayout
。一切都可以由 ConstraintLayout
来管理。您只需设置适当的视图约束。
这样做
<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">
<TextView
android:id="@+id/red"
android:background="@color/Red"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:text="Red" />
这就是 Android weight
分布应该如何工作。如果view
在LinearLayout
和horizontal orientation
中有权重,那么它只会在水平方向上获得importance/weight。与 LinearLayout
和 vertical orientation
的情况类似,视图在垂直方向上增加 importance/weight。这就是 android weight
分布的工作原理。
注意:使用width
(水平方向的情况下)或height
(垂直方向的情况)作为0dp
代替'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"
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" 和与屏幕左侧对齐。
从 TextView
中删除 android:layout_weight="1"
并检查。
注意:如果您使用的是ConstraintLayout
,则无需使用LinearLayout
。一切都可以由 ConstraintLayout
来管理。您只需设置适当的视图约束。
这样做
<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">
<TextView
android:id="@+id/red"
android:background="@color/Red"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:text="Red" />
这就是 Android weight
分布应该如何工作。如果view
在LinearLayout
和horizontal orientation
中有权重,那么它只会在水平方向上获得importance/weight。与 LinearLayout
和 vertical orientation
的情况类似,视图在垂直方向上增加 importance/weight。这就是 android weight
分布的工作原理。
注意:使用width
(水平方向的情况下)或height
(垂直方向的情况)作为0dp
代替'wrap_content' 以获得更好的性能。