我正面临着一个奇怪的问题。例如,我们有这样的布局:
<android.support.constraint.ConstraintLayout
...
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000">
<LinearLayout
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>我们的屏幕会变黑。如果我用FrameLayout或ConstraintLayout替换LinearLayout,结果是一样的。
但是如果用RelativeLayout替换first LinearLayout,屏幕将是红色的!在我的例子中,我需要像RelativeLayout这样的行为,但应该使用LinearLayout。这怎么可能呢?非常感谢!
发布于 2019-03-20 03:30:55
请尝试以下操作
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000">
<LinearLayout
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintVertical_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00"></LinearLayout>
</LinearLayout> </android.support.constraint.ConstraintLayout>
如果超级父代是LinearLayout而不是ConstraintLayout,请将app:layout_constraintVertical_weight="1"更改为android:layout_weight="1"并删除其他不必要的代码。如果超级父级是相对布局,那么只需删除"weight“参数,然后添加以下内容。
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"https://stackoverflow.com/questions/55246653
复制相似问题