Why LinearLayout's margin is being ignored if used as ListView row view

前端 未结 3 1766
一整个雨季
一整个雨季 2020-12-04 18:13

I was wondering, if I merely provide a single layer of LinearLayout as ListView\'s row view, its margin will be ignored.

Margin will be ignored if used

相关标签:
3条回答
  • 2020-12-04 18:23

    when you use a sigle layout this means this is your window if you apply margin on it then you'll asking for the margin of that amount from your parent view but we don't have parent view so margin won't work. on second place there is parent view and margin will help for internal view but not for external.

    0 讨论(0)
  • 2020-12-04 18:24

    Instead of adding a nested layout, you could use padding like so:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="5dp"
    
    0 讨论(0)
  • 2020-12-04 18:41

    The fact is that, the margin of LinearLayout (child) asks its parent layout (container) to give child layout a margin of x value.

    So if the parent layouts' LayoutParams support the margins then that margin is honored and applied.

    ListView uses AbsListView.LayoutParams by default, which doesn't include any margin support, just the height and width, thats why, it simply ignores the params value for margins.

    Whereas other layout params like ActionBar.LayoutParams, FrameLayout.LayoutParams, GridLayout.LayoutParams, LinearLayout.LayoutParams and RelativeLayout.LayoutParams are child of ViewGroup.MarginLayoutParams, which honors the child's margin values.

    0 讨论(0)
提交回复
热议问题