怎么在constraintlayout中左边控件设置gone后,让右边控件的marginStart失效
时间: 2024-03-02 20:50:44 浏览: 30
在 ConstraintLayout 中,如果左边控件设置为 `gone`,那么右边的控件会自动移动到左边控件的位置。如果你不想让右边控件的 marginStart 失效,可以在左边控件设置为 `gone` 后,再将右边的控件的约束条件中的 marginStart 属性设置为你想要的值即可。
例如,假设你有两个控件 A 和 B,A 在左边,B 在右边,B 的 marginStart 属性为 16dp。如果你希望在 A 设置为 `gone` 后,B 的 marginStart 仍然为 16dp,你可以在 A 设置为 `gone` 后,再设置 B 的约束条件如下:
```
app:layout_constraintStart_toEndOf="@id/A"
app:layout_constraintLeft_toRightOf="@id/A"
app:layout_constraintMarginStart="16dp"
```
这样,即使 A 不可见,B 仍然会保持在原来的位置,并且 marginStart 属性也会被保留。
相关问题
怎么在constraintlayout中不通过margin,让左右两个控件中间有空隙
在 ConstraintLayout 中,你可以通过在两个控件之间添加一个空的 `Guideline` 控件来实现控件之间的空隙,而不需要使用 margin。
下面是实现这个效果的步骤:
1. 添加一个 `Guideline` 控件,作为两个控件之间的分隔线,可以在布局编辑器中拖拽一个 `Guideline` 控件到布局中。
2. 将 `Guideline` 控件的方向设置为水平或垂直,具体根据你想要添加空隙的两个控件的位置而定。
3. 将 `Guideline` 控件的位置设置在两个控件的中间,可以通过拖拽或在属性面板中设置 `layout_constraintGuide_percent` 属性来实现。
4. 将两个控件的约束条件设置为分别与 `Guideline` 控件的左右或上下对齐,具体根据你设置的 `Guideline` 控件的方向而定。
例如,假设你有两个控件 A 和 B,你想在它们之间添加一个 16dp 的空隙。你可以按照以下步骤进行设置:
1. 在布局编辑器中添加一个水平或垂直的 `Guideline` 控件,位置设置在 A 和 B 之间的中心位置。
2. 将 A 的约束条件设置为与 `Guideline` 控件的右侧对齐,B 的约束条件设置为与 `Guideline` 控件的左侧对齐。
3. 在 `Guideline` 控件的属性面板中,设置 `layout_constraintGuide_percent` 属性为 0.5,并设置 `layout_constraintWidth_percent` 或 `layout_constraintHeight_percent` 属性为需要的值(例如 0.01),以便 `Guideline` 控件在布局中是可见的。
通过这样的设置,你就可以在 A 和 B 之间添加一个 16dp 的空隙,而不需要使用 margin。
ConstraintLayout 控件百分比设置
在 ConstraintLayout 中,可以使用百分比来设置控件的位置和大小。具体来说,可以使用以下属性:
1. layout_constraintWidth_percent:控件宽度占父布局宽度的百分比。
2. layout_constraintHeight_percent:控件高度占父布局高度的百分比。
3. layout_constraintLeft_toLeftOf、layout_constraintLeft_toRightOf、layout_constraintRight_toLeftOf、layout_constraintRight_toRightOf:控件左右边缘相对于父布局或其他控件的位置,可以使用百分比来设置。
4. layout_constraintTop_toTopOf、layout_constraintTop_toBottomOf、layout_constraintBottom_toTopOf、layout_constraintBottom_toBottomOf:控件上下边缘相对于父布局或其他控件的位置,可以使用百分比来设置。
例如,以下代码将一个 TextView 控件放置在父布局的中心位置:
```
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Hello World!"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintHeight_percent="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)