我有一个RecyclerView,它的底部边距是250dp。我需要在程序中将底部边距设置为0dp,然后再将其设置回250dp。我尝试了这个方法,但只能将其设置为0dp,无法将其设置回250dp:
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) linearLayoutRv.getLayoutParams();
params.setMargins(0,0,0,250); // params.setMargins(0,0,0,0); at first
linearLayoutRv.setLayoutParams(params);
这里有一些不必要的东西,但是以下是xml文件:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearLayoutRv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="250dp"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="51dp"
android:gravity="center">
//textview
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="51dp"
android:orientation="horizontal">
//some buttons
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginHorizontal="5dp"
android:padding="4dp"
android:scrollbars="vertical" />
</LinearLayout>