【问题标题】:How to set margin to main ConstraintView in android?如何在android中将边距设置为主ConstraintView?
【发布时间】:2020-05-25 15:31:17
【问题描述】:

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

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>

在我的情况下如何设置边距?

【问题讨论】:

  • mainLayout 是哪一个?? detail_feed_scool_view??
  • 是的。 var mainLayout = findViewById(R.id.detail_feed_scrool_view)
  • 我认为您对为什么会感兴趣,不是吗?所以我提供了详细的答案
  • 我尝试使用 FrameLayout.LayoutParams。只是错误消息改变如下: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
  • 我在回答中解释了LayoutParams的概念,请立即查看答案,您将能够解决您的问题

标签: java android kotlin layoutparams layoutmargins


【解决方案1】:

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

例如:-

如果您尝试从您的&lt;View&gt; 获取布局参数,该android:id="@+id/shadow_view" 的ID 为android:id="@+id/shadow_view",那么layoutParams 将是ConstraintLayout.LayoutParams 类型。

为什么?因为 ConstraintLayout&lt;View&gt; 的父布局。现在&lt;ViewPager&gt;也是如此,它的直接父级也是ConstraintLayout,所以同样ConstraintLayout.LayoutParams

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

现在回答您的问题

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

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

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

类似的东西

<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 类型获取

【讨论】:

  • 好的,我会试试这个答案。谢谢你。当我尝试您的建议时,我会反馈到这里。你的回答对我来说很有意义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-19
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 2017-02-01
  • 2016-12-04
相关资源
最近更新 更多