[英]How to set margin to main ConstraintView in android?
我想以編程方式為我的主要布局設置邊距。 我使用布局參數作為以下代碼。
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>
如何在我的情況下設置保證金?
布局/視圖的LayoutParams
取決於它的父布局
例如:-
如果您嘗試從具有 id android:id="@+id/shadow_view"
的<View>
獲取布局參數,則layoutParams
將是ConstraintLayout.LayoutParams
類型。
為什么? 因為ConstraintLayout
是<View>
的父布局。 現在同樣適用於<ViewPager>
,它的直接父級也是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 類型獲取
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.