2

我想以编程方式为我的主布局设置边距。我使用布局参数作为以下代码。

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>

如何在我的情况下设置保证金?

4

1 回答 1

1

布局/视图LayoutParams取决于它的父布局

例如:-

如果您尝试从<View>拥有 id的人那里获取布局参数,android:id="@+id/shadow_view"那么layoutParams将是ConstraintLayout.LayoutParams类型。

为什么?因为ConstraintLayout是 的父布局<View>。现在同样适用<ViewPager>,它的直接父母也是ConstraintLayout,所以同样ConstraintLayout.LayoutParams

并且当您想layoutParams在任何视图/布局上进行设置时,请始终查找直接父布局并设置为与该父布局类型相同layoutParams

现在回答你的问题

在您的情况下,您没有任何父布局,这就是您获得ViewGroup.LayoutParams.

ViewGroup是布局的父类,更像是一个泛型类,这个类没有边距。

对您来说最简单的解决方案就是将您的整个布局包装在您想要的另一个布局中,因此您将有一个父级,ConstraintLayoutLayoutParams将是您用来包装的布局类型

像这样的东西

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout 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/colorPrimary">

        <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>
</LinearLayout>

现在,当您尝试获取时,LayoutParams您会将它们作为 LinearLayout 类型

于 2020-02-10T11:21:30.023 回答