简体   繁体   中英

how to change view constraint top margin programmatically in Android?

I am using constraint layout and I have a view like this

<View
           android:id="@+id/view_separator_line"
           android:layout_width="0dp"
           android:layout_height="1dp"
           android:layout_marginTop="32dp"
           android:background="@color/grey_dark_low"
           app:layout_constraintEnd_toEndOf="@+id/imageView_gear_setting_user_information"
           app:layout_constraintStart_toStartOf="@+id/imageView_following_profile_picture"
           app:layout_constraintTop_toBottomOf="@+id/imageView_following_profile_picture" />

as you can see from the xml above, top of this view is constrained to the bottom of another view, and the layout margin top is 32dp, I want to change it programmatically to be 24dp, how to do that?

try this below code

ConstraintLayout.LayoutParams newLayoutParams = (ConstraintLayout.LayoutParams) toolbar.getLayoutParams();
     newLayoutParams.topMargin = 0;
     newLayoutParams.leftMargin = 0;
     newLayoutParams.rightMargin = 0;
     toolbar.setLayoutParams(newLayoutParams););

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM