在 ConstraintLayout 之外定位视图
Position view outside of ConstraintLayout
我想将视图放置在 ConstraintLayout
之外,以便使用滑动动画来为它们设置动画。我试过设置像 constraintBottom_toTopOf="parent"
这样的约束,但 View
留在容器内。
请注意,我想通过限制使用 built-in 动画来实现此目的,而不是使用 in-code 动画。
知道我该怎么做吗?
我正在使用 compile 'com.android.support.constraint:constraint-layout:1.1.0-beta1'
和 Android Studio 3.0 Beta 7
这是一个简单的 xml 文件,应该将视图放在容器之外:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorAccent">
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@color/colorPrimary"
app:layout_constraintBottom_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
但这是结果
在每个视图中都可以使用负边距,将视图放在父视图之外,然后设置裁剪参数。
android:clipChildren="false"
android:clipToPadding="false"
这将使视图不被剪裁。
这似乎是 ConstraintLayout
1.1.0-beta1 的问题;它在 ConstraintLayout
1.1.0-beta3.
中按预期工作
更新到 ConstraintLayout
1.1.0-beta3。我还会注意到,您需要通过执行以下操作来水平限制视图。
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@color/colorPrimary"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="parent" />
附带说明,ConstraintLayout
不接受负边距。请参阅 有关负边距的 Stack Overflow 问题和 ConstraintLayout
。
我得到了另一种解决问题的方法:
1.Add一个主播(anchor_left
)layout_constraintStart_toStartOf="parent"
.
2.Add YourView
layout_constraintEnd_toStartOf="@+id/anchor_left"
就是这样!
代码:
<android.support.constraint.ConstraintLayout>
<View
android:id="@+id/anchor_left"
app:layout_constraintStart_toStartOf="parent"/>
<YourView
android:id="@+id/ll_left"
app:layout_constraintEnd_toStartOf="@+id/anchor_left"/>
</android.support.constraint.ConstraintLayout>
我做的是:
- 在 ConstraintLayout 中创建了一个 0dp 高度的视图,例如"fakeView"
- 将新的 fakeView 锚定在 ConstraintLayout 的顶部
当我需要隐藏视图时,将其转换到约束之外。
- 更改要隐藏的视图的约束,以便将 BOTTOM 连接到 FakeView 的顶部。
我认为您可以使用相同的技术在假视图的左侧或右侧移动对象。
一个技巧是在 ConstraintLayout
本身中为您想要的一侧设置负边距。这需要对那一侧有约束的其他视图进行偏移:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
...
android:layout_marginBottom="-48dp">
<ImageButton
android:id="@+id/leftButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="72dp"
android:background="@drawable/shape_next_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<ImageButton
android:id="@+id/rightButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginStart="24dp"
android:background="@drawable/shape_previous_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我想将视图放置在 ConstraintLayout
之外,以便使用滑动动画来为它们设置动画。我试过设置像 constraintBottom_toTopOf="parent"
这样的约束,但 View
留在容器内。
请注意,我想通过限制使用 built-in 动画来实现此目的,而不是使用 in-code 动画。
知道我该怎么做吗?
我正在使用 compile 'com.android.support.constraint:constraint-layout:1.1.0-beta1'
和 Android Studio 3.0 Beta 7
这是一个简单的 xml 文件,应该将视图放在容器之外:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorAccent">
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@color/colorPrimary"
app:layout_constraintBottom_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
但这是结果
在每个视图中都可以使用负边距,将视图放在父视图之外,然后设置裁剪参数。
android:clipChildren="false"
android:clipToPadding="false"
这将使视图不被剪裁。
这似乎是 ConstraintLayout
1.1.0-beta1 的问题;它在 ConstraintLayout
1.1.0-beta3.
更新到 ConstraintLayout
1.1.0-beta3。我还会注意到,您需要通过执行以下操作来水平限制视图。
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@color/colorPrimary"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="parent" />
附带说明,ConstraintLayout
不接受负边距。请参阅 ConstraintLayout
。
我得到了另一种解决问题的方法:
1.Add一个主播(anchor_left
)layout_constraintStart_toStartOf="parent"
.
2.Add YourView
layout_constraintEnd_toStartOf="@+id/anchor_left"
就是这样!
代码:
<android.support.constraint.ConstraintLayout>
<View
android:id="@+id/anchor_left"
app:layout_constraintStart_toStartOf="parent"/>
<YourView
android:id="@+id/ll_left"
app:layout_constraintEnd_toStartOf="@+id/anchor_left"/>
</android.support.constraint.ConstraintLayout>
我做的是:
- 在 ConstraintLayout 中创建了一个 0dp 高度的视图,例如"fakeView"
- 将新的 fakeView 锚定在 ConstraintLayout 的顶部
当我需要隐藏视图时,将其转换到约束之外。
- 更改要隐藏的视图的约束,以便将 BOTTOM 连接到 FakeView 的顶部。
我认为您可以使用相同的技术在假视图的左侧或右侧移动对象。
一个技巧是在 ConstraintLayout
本身中为您想要的一侧设置负边距。这需要对那一侧有约束的其他视图进行偏移:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
...
android:layout_marginBottom="-48dp">
<ImageButton
android:id="@+id/leftButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="72dp"
android:background="@drawable/shape_next_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<ImageButton
android:id="@+id/rightButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginStart="24dp"
android:background="@drawable/shape_previous_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>