Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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?

like image 811
Cheok Yan Cheng Avatar asked Apr 29 '13 12:04

Cheok Yan Cheng


2 Answers

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.

like image 73
Adil Soomro Avatar answered Oct 20 '22 06:10

Adil Soomro


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.

like image 23
Tashen Jazbi Avatar answered Oct 20 '22 04:10

Tashen Jazbi