首页 > 解决方案 > ConstraintLayout layoutParams setMargin() notWorking

问题描述

我有一个浮动操作按钮,我想以编程方式更改其位置

<com.google.android.material.floatingactionbutton.FloatingActionButton
   android:id="@+id/expandViewButton"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:clickable="true"
   android:visibility="visible"
   android:layout_marginStart="168dp"
   app:layout_constraintStart_toStartOf="@+id/coordinatorLayout1"
   app:layout_constraintTop_toTopOf="@+id/coordinatorLayout1"
   app:srcCompat="@mipmap/ic_launcher_round" />

我使用以下代码以编程方式更改位置。

ConstraintLayout.LayoutParams layoutParams=new 
ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,ConstraintLayout.LayoutParams.WRAP_CONTENT);
int left,right,top,bottom;
left=(int)MainActivity.device_width/2;
right=0;
top=(int) MainActivity.device_height-200;
bottom=0;
layoutParams.rightMargin=right;
layoutParams.topMargin=20;
layoutParams.leftMargin=left;
layoutParams.bottomMargin=bottom;
   
layoutParams.bottomToBottom=R.id.fb1;
layoutParams.startToStart=R.id.fb1;

但是layoutParams.rightMarginlayoutParams.topMargin和似乎不起作用layoutParam.leftMarginlayoutParams.bottomMargin请建议我一个解决方案。

标签: javaandroid

解决方案


推荐阅读