【问题标题】:How to add layout rules in constraint layout如何在约束布局中添加布局规则
【发布时间】:2018-07-17 21:11:05
【问题描述】:

我正在使用this 库在捕获的图像上添加贴纸。

问题是,这个库正在使用Relative Layout。我需要将其转换为约束布局,但一旦我这样做,它就会停止工作。

这就是用户在image view上添加贴纸时所做的事情

StickerView stickerView = new StickerView(this);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);
    params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.image);
    params.addRule(RelativeLayout.ALIGN_TOP, R.id.image);
    ((ViewGroup) mImageView.getParent()).addView(stickerView, params);

当我将此布局转换为 constraint layout 时,我写了这个。

   RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);

但是约束布局没有params.addRule 函数,我无法理解如何从代码中在约束布局上添加元素。

约束布局

<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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_photo_editor">


        <ImageView
            android:id="@+id/effect_image"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/grayCheckout"
            android:scaleType="centerInside"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <ImageView
            android:id="@+id/image"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:background="#90000000"
            android:padding="5dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

        </android.support.v7.widget.RecyclerView>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@android:color/holo_blue_dark" />


    </android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 父级是ConstraintLayout?否则你不能添加另一个参数
  • 是的,我已将父布局设置为约束,
  • 您需要添加布局xml(或代码)以便我们提供帮助
  • @MarcosVasconcelos :我已经添加了 xml,每次用户点击贴纸时,我都需要在约束布局中添加它。贴纸来自列表视图

标签: android android-layout layout android-constraintlayout


【解决方案1】:

要以编程方式放置ConstraintLayout 视图,您需要使用ConstraintSet

此类允许您以编程方式定义一组要与 ConstraintLayout 一起使用的约束。它允许您创建和保存约束,并将它们应用到现有的 ConstraintLayout。

网上有一些很好的教程解释了如何使用这个工具。只需搜索ConstraintSet

【讨论】:

    猜你喜欢
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 2020-07-10
    相关资源
    最近更新 更多