【发布时间】: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