Hei*_*nzi 16 android margin collapse android-layout
是否有可能在Android中使利润率崩溃?比方说,我有一个LinearLayout和添加三个TextView,每个具有一个android:layout_margin的10dp.我得到以下结果:

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

我知道我可以通过为不同的项目设置不同的上/下边距来解决这个问题:
但这会使设计更复杂(特别是如果动态创建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)