我试图将具有4个相同大小的按钮的水平LinearLayout
转换为ConstraintLayout
。 问题是,当我在LinearLayout
中设置一个或多个按钮到android:visibility="gone"
时,其余按钮的大小将调整为占用整个空间(全部大小相同),并在ConstraintLayout
中删除按钮,但仍占用空间。将LinearLayout转换为ConstraintLayout问题
编辑:根据应用程序状态,不同的按钮将是可见的。
我需要更改什么,以便ConstraintLayout
的行为与LinearLayout
相似?
编辑:我发现ConstraintLayout(约束引用)的错误,所以我更新它和图像(问题仍然存在)。
LinearLayout
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/b1"
android:text="B1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="50"
/>
<Button
android:id="@+id/b2"
android:text="B2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="50"
android:visibility="gone"
/>
<Button
android:id="@+id/b3"
android:text="B3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="50"
/>
<Button
android:id="@+id/b4"
android:text="B4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="50"
/>
</LinearLayout>
编辑:ConstraintLayout
XML:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/b1"
android:text="B1"
android:layout_width="0px"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/b2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_weight="1"
/>
<Button
android:id="@+id/b2"
android:text="B2"
android:layout_width="0px"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintLeft_toRightOf="@+id/b1"
app:layout_constraintRight_toLeftOf="@+id/b3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_weight="1"
/>
<Button
android:id="@+id/b3"
android:text="B3"
android:layout_width="0px"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/b2"
app:layout_constraintRight_toLeftOf="@+id/b4"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_weight="1"
android:layout_marginTop="0dp"/>
<Button
android:id="@+id/b4"
android:text="B4"
android:layout_width="0px"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/b3"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_weight="1"
/>
</android.support.constraint.ConstraintLayout>
为什么要替换线性布局,如果它工作正常?它慢吗?它吃了很多内存吗? – pskink
我有一个复杂的布局结构,所以我将它转换为约束布局。这是我没有设法转换的一部分。 – Hanan
我知道我可以在约束布局中嵌入线性布局,但是想避免它(根据我的理解约束布局的目的是去除深布局层次结构)。 – Hanan