34

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 of ListView's row view

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="5dp"

However, if I provide double layer of LinearLayout, with first layer acted as "dummy" layer, its margin will not be ignored.

We will have margin in ListView's row view

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/buyPortfolioLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"

May I know why it happen so?

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • My observation is that all attributes with the _layout_ prefix refer to this UI element's relationship with its parent and can be sort of overridden by the parent, whereas attributes without _layout_ refer to this UI element's relationship with its children. Have you tried "padding" instead of "layout_margin"? – class stacker Apr 29 '13 at 12:08
  • Same. Either using margin or padding at the outer most of LinearLayout will be ignored too. – Cheok Yan Cheng Apr 29 '13 at 12:13

3 Answers3

68

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.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • Sorry to bump this. I have a linear layout with textViews and I set the `weight` property there. When I use that layout as my listRow view (programatically), the weight property is ignored and my textViews are pushed next to one another. – Razgriz Aug 07 '14 at 05:22
  • @Razgriz you should open a new question and provide detail and code there as this seems to be a different query. – Adil Soomro Aug 07 '14 at 06:23
  • Here: http://stackoverflow.com/questions/25159494/android-textviews-weight-property-in-linearlayout-is-being-ignored-when-used – Razgriz Aug 07 '14 at 10:36
  • Is there a way to override this behavior in LinearLayout such that changing the width (say) of a view dynamically would ignore the margins and change its width? – Divins Mathew Jun 04 '17 at 20:08
  • @Divins I'm afraid no, you would've to manually set the width and margins. – Adil Soomro Jun 04 '17 at 20:18
  • 1
    ListView understands [`android:padding`](https://developer.android.com/reference/android/view/View.html#attr_android:padding), so if possible (depends on your layout structure and your's goals) you can use android:padding property. Nowadays the ideal it's use the [`RecyclerView`](https://developer.android.com/guide/topics/ui/layout/recyclerview) – GFPF Jun 04 '19 at 21:50
1

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.

Tashen Jazbi
  • 1,068
  • 1
  • 16
  • 41
-1

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"
unify
  • 6,161
  • 4
  • 33
  • 34
  • That won't work because the question states that having a single layout doesn't apply the padding but nesting does. I've just ran into the same problem. – Tim Kist Sep 11 '14 at 08:56