Viv*_*odi 4 android android-layout android-linearlayout android-constraintlayout
我有一个线性布局。在家长看来,我已经定义了填充。但我想从子视图中删除填充。是否可以删除中间子视图填充?我知道一种解决方案是我需要单独提供填充。但我不想使用这个解决方案。所以任何替代解决方案。
布局
<?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>
Run Code Online (Sandbox Code Playgroud)
上面的代码看起来像这样
预期输出
这可以通过添加 LinearLayout 来完成
android:clipToPadding="false"
Run Code Online (Sandbox Code Playgroud)
并在视图中设置负水平边距:
android:layout_marginStart="-16dp"
android:layout_marginEnd="-16dp"
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为你有一个 LinearLayout,除了 LinearLayout 和relativelayout 之外,其他 ViewGroup 似乎不支持负边距,正如这个答案所说:https ://stackoverflow.com/a/10673572/7794806