【发布时间】:2021-12-21 12:21:57
【问题描述】:
我有一个线性布局。在家长看来,我已经定义了填充。但我想从子视图中删除填充。是否可以删除中间子视图填充?我知道一种解决方案是我需要单独提供填充。但我不想使用这个解决方案。所以任何替代解决方案。
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tool="http://schemas.android.com/tools"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:visibility="gone"
tool:text="Header text" />
<View
android:id="@+id/separator"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="10dp"
android:background="#cccccc"
android:visibility="gone"
app:layout_constraintTop_toBottomOf="@id/header" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/dialog_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp" />
</LinearLayout>
上面的代码是这样的
预期输出
【问题讨论】:
-
如果您不希望它具有通用性,答案是不要在父级上放置填充。相反,在元素上放置边距。如果因为元素很多而这很困难,请使用第二个布局将它们分组并将其放在组中。
标签: android android-layout android-linearlayout android-constraintlayout