Android: margin / padding for ListView without applying margin to title?

Is it possible to have margin / padding for a ListView without applying a field to the title? I want my ListView to look like a Google Now layout. I have everything except this little problem.

Question: Is it possible that the ListView layout options do not apply to its title?

EDIT1: Additional Information

The purple view is the title of the listView. I tried to add margin to the list item layout. This does not work either. All I need is a way to apply the field to the list so that it does not apply the marker to the title in the listView.

EDIT2: My header.xml layouts

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:orientation="vertical" > <View android:layout_width="match_parent" android:layout_height="@dimen/top_height" android:background="@color/top_item" /> <View android:id="@+id/placeholder" android:layout_width="match_parent" android:layout_height="@dimen/sticky_height" android:layout_gravity="bottom" /> </FrameLayout> 

root_list.xml

 <LinearLayout> <com.abhijith.CustomListView android:id="@android:id/list" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_height="wrap_content" /></LinearLayout> 

list_item.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/card_background" android:orientation="vertical" > <TextView android:id="@+id/textview_note" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/textview_note_date" android:layout_width="match_parent" android:layout_height="wrap_content"/></LinearLayout> 

I removed the layout properties here due to the length of this post. This is how it looks

+4
source share
4 answers

After searching many times, I found that it was impossible to separate it from the parameters of the listView list from its headers, and the title was part of the list. Thus, in order to get the effect of desirred margin, after many looking around, I found that adding a field to the root of the layout in the list view item did not help. So I added another dummy LinearLayout inside the existing root LinearLayout (nested LinearLayout) and added children inside the nested layout. Now the settings field for the internal layout gives the desired effect.

code:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="16dp" android:layout_marginLeft="16dp" android:background="@drawable/list_selector" android:orientation="vertical" > <TextView android:id="@+id/textview_note" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/textview_note_date" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> 
+7
source

In your header part, add the following tag:

 android:layout_alignParentLeft="true" 

this will solve your problem.

0
source
 // Temp is the root view that was found in the xml final View temp = createViewFromTag(root, name, attrs, false); ViewGroup.LayoutParams params = null; if (root != null) { if (DEBUG) { System.out.println("Creating params from root: " + root); } // Create layout params that match root, if supplied params = root.generateLayoutParams(attrs); if (!attachToRoot) { // Set the layout params for temp if we are not // attaching. (If we are, we use addView, below) temp.setLayoutParams(params); } // We are supposed to attach all the views we found (int temp) // to root. Do that now. if (root != null && attachToRoot) { root.addView(temp, params); } // Decide whether to return the root that was passed in or the // top view found in xml. if (root == null || !attachToRoot) { result = temp; } ... return result; 

this is the LayoutInflater code, as you can see, if you added root , then it is not null, generated layoutParams from root.

and then if the thrid parameter is false , there is only make temp.setLayoutParams(params) , which params generated by root! and pace as a return ....

if attachToRoot true , root will execute root.addView (temp, params), and then the root is returned.

if you just do LayoutInflater.from(this).inflate(R.layout.popup_choose_type, null); , and if you set some attribute as paddingBottom , it will surprise you that this will not work !! due to the loss of LayoutParams !

How to decide, you can see here

0
source

you can do this by providing a margin in the list layout file, then you will get it.

-1
source

All Articles