折叠Android布局中的边距

Hei*_*nzi 16 android margin collapse android-layout

是否有可能在Android中使利润率崩溃?比方说,我有一个LinearLayout和添加三个TextView,每个具有一个android:layout_margin10dp.我得到以下结果:

实际结果

但是,我想得到这个结果:

预期结果

我知道我可以通过为不同的项目设置不同的上/下边距来解决这个问题:

  • 将第一个项目的上边距和最后一个项目的下边距设置为10dp,
  • 将剩余的上/下边距设置为5dp,

但这会使设计更复杂(特别是如果动态创建TextView).有没有办法让边距在CSS中表现得像?(有关为什么这有意义的解释,请参阅:CSS折叠边距的重点是什么?)

kco*_*ock 15

我自己通常要做的就是简单地将View的(即你的TextView)边距切成两半,然后将相同的数字添加到包含ViewGroup的数字(即你的LinearLayout).这样,您将在所有项目周围均匀间距.例如:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dip"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dip"
        android:text="I'm a TextView!"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dip"
        android:text="I'm a TextView!"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dip"
        android:text="I'm a TextView!"
        />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

  • 我很惊讶这仍然是迄今为止我找到的最佳答案。它仅适用于此用例,但不适用于元素更改高度的复杂布局 (2认同)