我想以编程方式为我的主布局设置边距。我使用布局参数作为以下代码。
val parMain = mainLayout?.layoutParams as ConstraintLayout.LayoutParams
但我有一些错误,比如
android.view.ViewGroup$LayoutParams 无法转换为 androidx.constraintlayout.widget.ConstraintLayout$LayoutParams
如果我按照 Android 的要求更改代码,如下所示:
val parMain = mainLayout?.layoutParams as ViewGroup.LayoutParams
我的 layoutParams 中没有设置边距功能。
那是我的xml代码。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/detail_feed_scrool_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent"
tools:context=".DetailActivity">
<View
android:id="@+id/shadow_view"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="#99000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/detail_feed_view_pager_activity"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.viewpager.widget.ViewPager>
<FrameLayout
android:id="@+id/detail_fragment_frame_layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/white"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
如何在我的情况下设置保证金?