Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android child view ignore parent padding

Tags:

I have many layouts that have different ViewGroup roots (LinearLayouts, RelativeLayouts, etc). These views all work as I expect, but now I am trying to add a top navigation bar. The problem is that the roots can have different paddings, and if I simply include my layout within each of these layouts, the navigation bar is limited in width by the parent's padding. I'd like this navigation bar to ignore the root's left/right/top padding and be completely full width at the very top of the layout. Is there anything I can do within the navigation bar's layout to achieve this, or am I doomed to have to modify all of the existing layouts to accommodate this?

like image 372
Nelson Monterroso Avatar asked Apr 15 '11 18:04

Nelson Monterroso


2 Answers

While this seems like a pretty bad idea to begin with, you might be able to accomplish what you're looking for by setting the clipToPadding attribute of the parent ViewGroup to false and then set negative margins on the child view.

Example:

<LinearLayout     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:padding="10dp"     android:background="#EEE"     android:clipToPadding="false">     <TextView           android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="@string/hello"         android:background="#333"         android:layout_marginLeft="-10dp"         android:layout_marginRight="-10dp"         android:layout_marginTop="-10dp"/> </LinearLayout> 

The above example works, but I would suggest that you should just put your top navigation bar outside of these ViewGroup that contain the paddings you're trying to avoid.

like image 132
Rico Yao Avatar answered Sep 20 '22 13:09

Rico Yao


Why would you need to change all of your layouts? If it's only your navigation bar that needs to ignore the padding, you could just take the nav bar out of it's current parent layout, and then put both layouts inside another layout that has no padding.

like image 20
BigFwoosh Avatar answered Sep 16 '22 13:09

BigFwoosh