我遇到了一个奇怪的问题。 例如,我们有这样的布局:
<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>
我们的屏幕将变黑。如果我将 LinearLayout 替换为 FrameLayout 或 ConstraintLayout,结果相同。
但是如果先用 RelativeLayout 替换 LinearLayout 屏幕会变红! 在我的例子中,我需要类似 RelativeLayout 的行为,但应该使用 LinearLayout。 怎么可能? 非常感谢!
最佳答案
尝试以下操作
<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>
如果 super 父级是 LinearLayout 而不是 ConstraintLayout,请将 app:layout_constraintVertical_weight="1"
更改为 android:layout_weight="1"
并删除其他不必要的代码。如果 super 父级是相对布局,则只需删除“weight”参数并添加以下内容。
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
关于android - 具有嵌套 ViewGroup 的 ViewGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55246653/