约束布局外的位置视图

9

我想将视图放置在ConstraintLayout之外,以便使用滑动动画来对它们进行动画处理。我尝试设置像constraintBottom_toTopOf="parent"这样的约束条件,但是View仍会留在容器内。

请注意,我想使用约束条件实现此目的,以使用内置动画,而不是使用代码中的动画。

你有任何想法吗?

我正在使用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>

但这是结果
输入图像描述

我从未尝试过,但是把偏差从0变到1怎么样? - Juan
请分享你的XML。 - Muhammed Refaat
请在帖子中注明您正在使用的ConstraintLayout版本。确保您没有定义额外的约束条件,这些条件会将视图拉回到布局中。此外,查看XML文件会很有帮助。 - Cheticamp
我添加了XML和ConstraintLayout版本。 - David Seroussi
我通过设置padding和clipToPadding="false"使其正常工作。我有一个ImageView,我按百分比相对于Guideline水平定位,并且需要在0-1范围内工作。当在ConstraintLayout的右/左边缘时,imageView被剪切了一半。通过为ConstraintLayout设置rightPadding和leftPadding为ImageView宽度的一半,它不再被剪切(并且百分比定位仍然有效)。 - PhoneyDeveloper
5个回答

5

这似乎是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,请参考这个Stack Overflow问题。


我接受了这个答案,因为它解决了这个特定的问题,但我知道它会带回一个旧问题,即ConstraintLayout在嵌套的ScrollView中被搞乱了。 - David Seroussi
@DavidSeroussi 很抱歉听到又出现了问题。您可以发布一个带有导致问题的XML的嵌套滚动视图问题的问题。 - Cheticamp

3
在每个视图中,您都可以使用负边距将视图放置在父视图之外,然后设置剪切参数。
android:clipChildren="false"
android:clipToPadding="false"

这将使视图不被裁剪。

负边距在约束布局中不起作用,这一点在问题中已经提到。 - Pure Function

2

我有另一种解决问题的方法:

1.添加一个锚点(anchor_left) layout_constraintStart_toStartOf="parent"

2.将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>

锚定视图对我也起了作用。我只是控制锚定视图而不是视图本身。直接将约束放在“父项”上会使其超出父项,但它只会固定在那个位置。 - Mon

0
我所做的是:
  • 在ConstraintLayout中创建一个高度为0dp的视图,例如“fakeView”
  • 将新的fakeView放置在ConstraintLayout的顶部

当我需要隐藏一个视图时,将其转移到约束之外..

  • 更改要隐藏的视图的约束,以使底部连接到FakeView的顶部。

我认为您可以使用相同的技术将对象移动到fakeview的左侧或右侧。


0
一个技巧是在ConstraintLayout本身中为您想要的一侧设置负边距。这需要对那一侧有约束的其他视图进行偏移:

enter image description here enter image description here

<?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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接