如何在约束布局中添加布局规则

时间:2018-02-07 14:22:30

标签: android android-layout layout android-constraintlayout

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

1 个答案:

答案 0 :(得分:1)

要以编程方式放置ConstraintLayout次观看,您需要使用ConstraintSet

  

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

网上有一些很好的教程可以解释如何使用这个设施。只需搜索ConstraintSet

相关问题