考虑此布局:
<?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
的方向时,效果相反:TextView
从顶部延伸到底部,但其宽度受字符串“ Red”限制,并与屏幕左侧。
答案 0 :(得分:2)
从android:layout_weight="1"
中删除TextView
并进行检查。
注意:如果您使用的是ConstraintLayout
,则无需参加LinearLayout
。一切都可以由ConstraintLayout
管理。您只需要设置适当的视图约束即可。
答案 1 :(得分:1)
这就是应该运行Android weight
发行版的方式。如果view
在LinearLayout
中具有horizontal orientation
的权重,则它将仅在水平方向获得重要性/权重。 LinearLayout
与vertical orientation
的情况类似,视图在垂直方向上的重要性/权重增加。这就是android weight
发行版的工作方式。
注意:将width
(在水平方向上)或height
(在垂直方向上)用作 0dp
而不是 wrap_content'。
答案 2 :(得分:0)
这样做
<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" />
答案 3 :(得分:0)
您的text-view
未被拉伸到全屏,而是由gravity
设置为居中,这就是为什么它看起来像是水平拉伸而不是垂直拉伸的原因。
并且您已将其放置在线性布局中,该布局会根据相应的方向设置子线虎钳,这就是为什么它不会垂直拉伸的原因。