【问题标题】:Can I put margin to a container element in Android?我可以在 Android 中为容器元素设置边距吗?
【发布时间】:2013-11-20 16:10:21
【问题描述】:

我想知道是否可以在父级中定义子级的边距。

像这样:

<LinearLayout
childrenMargin="10dp"
>
 <child />
 <child />
 <child />
</LinearLayout>

还是我必须为儿童设置layout_margin

【问题讨论】:

    标签: android xml layout styles margin


    【解决方案1】:

    听起来您想为父元素添加内边距。

    <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:padding="10dp" >
    
         ...
    
    </LinearLayout>
    

    一种思考方式是,padding 是 View 的内部间距(即它与其子项之间的间距),而 ma​​rgin 是 View 的外部间距(它与其父级和邻居之间的空间)。

    【讨论】:

    • 谢谢,但我有儿童背景,所以我需要单独指定填充。所以我需要保证金,但我只是想知道我是否可以将它从父母传递给孩子。
    • 如果你绝对必须有一个margin,那么你必须手动设置它;没有办法为所有孩子指定属性。但是,即使使用边距代替填充,子元素的背景仍然会符合边距;你会得到相同的结果。
    • 好的,我会试试的。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2022-12-06
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    相关资源
    最近更新 更多