android child view ignore parent padding-ThrowExceptions

Exception or error:

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?

How to solve:

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.

###

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.

###

Actually, turns out you can alter the default title bar: http://www.helloandroid.com/tutorials/how-create-custom-titlebar.

http://www.anddev.org/my_own_titlebar_backbutton_like_on_the_iphone-t4591.html also proved useful. Thanks anyways!

Leave a Reply

Your email address will not be published. Required fields are marked *