17

I have FrameLayout and for most fragments the parrent padding is good. But I have especial fragment which must not contain the padding.

<FrameLayout
    android:id="@+id/frame_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    />

<!-- My especial fragment -->
<LiearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="-10dp"
/>

Any suggestions!

Coffeeman
  • 285
  • 1
  • 6
  • 17

2 Answers2

32

Curious, just using layout_margin does not seem to work. However,

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:padding="10dp"
         android:clipToPadding="false">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="-10dp"
        android:layout_marginStart="-10dp"
        android:layout_marginEnd="-10dp"
        android:layout_marginBottom="-10dp" />
</FrameLayout>

seems to work (IntelliJ android preview).

Do not forget: android:clipToPadding="false" in the parent layout.

Sascha Kolberg
  • 7,092
  • 1
  • 31
  • 37
  • Error:(3) No resource identifier found for attribute 'margin' in package 'android' – Coffeeman Aug 09 '15 at 17:47
  • ups, sorry, fixed the answer. – Sascha Kolberg Aug 09 '15 at 17:50
  • While not an explicit answer to my question, this was useful. The parent of a TextView uses a Drawable as its background. The presence of the drawable background automatically added padding to the child TextView, and this was solved by setting the parent padding to 0dp. – Jason McClinsey Sep 14 '19 at 22:14
1

Seems like things would be easier if you would just remove the android:padding="10dp" from your FrameLayout and in your "normal" fragments you add the 10dp padding, while in your "special" fragment you don't add any padding. Negative margins can be done, but in my opinion should only be used if there is no other way to do it.

donfuxx
  • 11,277
  • 6
  • 44
  • 76
  • Yes, it is one of the way, but it is routine. Another way it is set padding programmatically, when I espesial fragment selected. But another hand more elegant do it without code – Coffeeman Aug 09 '15 at 18:01