Han*_*nan 10 android android-layout android-constraintlayout
我试图将LinearLayout具有4个相同大小的按钮的水平转换为a ConstraintLayout.问题是当我设置一个或多个按钮时android:visibility="gone",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>
Run Code Online (Sandbox Code Playgroud)
编辑: 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>
Run Code Online (Sandbox Code Playgroud)
Ahm*_*mad 12
您可以轻松地将任何布局转换为a ConstraintLayout,只需按照以下说明操作:

您可能可以将布局更改为:
<?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"
android:orientation="horizontal">
<Button
android:id="@+id/b1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="B1"
app:layout_constraintBaseline_toBaselineOf="@+id/b3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/b3"
tools:layout_constraintBaseline_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1" />
<Button
android:id="@+id/b2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="B2"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<Button
android:id="@+id/b3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="128dp"
android:layout_marginLeft="128dp"
android:layout_marginRight="128dp"
android:layout_marginStart="128dp"
android:text="B3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<Button
android:id="@+id/b4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="B4"
app:layout_constraintBaseline_toBaselineOf="@+id/b3"
app:layout_constraintLeft_toRightOf="@+id/b3"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintBaseline_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
如果您在将现有布局切换为 时遇到困难ConstraintLayout,您可以继续尝试 Android Studio 的内部设计工具来帮助您解决这个问题。您可以切换到“设计”选项卡并打开“组件树”窗口,右键单击要转换的元素并选择“转换为 ConstraintLayout”。
| 归档时间: |
|
| 查看次数: |
10398 次 |
| 最近记录: |