Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

margin or padding not working using <merge tag android

Tags:

android

xml

I am using merge tag in custom view and trying to set layout_marginTop or paddingTop on parent view, but doesn't work. Here is my code file name: my_view.xml

<merge
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android_layout_marginTop="17dp" >  // This margin is lost!
    // content here  ...
</merge>

So in My custom view file, MyView.java, I just inflate this my_view.xml. But the android_layout_marginTop information is lost.

public class MyView {
    private void initViews() {
        View view = LayoutInflator.from(context).inflate(R.layout.my_view, this);
        // get rest views
    }
}

I have tried to set the margins and paddings in the code using LayoutParams, but still not working. Your help would be much appreciated!

like image 954
Cheng Avatar asked Aug 10 '17 20:08

Cheng


Video Answer


1 Answers

The <merge> tag is not a View, so trying to set a margin on it isn't going to work.

It looks like you're following the "Compound Control" pattern, where you create a subclass of some common ViewGroup (a LinearLayout, perhaps) and then inflate some other standard Views into it. If that is the case...

Remove all of the layout_ attributes from your <merge> tag, and instead place them on your custom view's tag, wherever you use it in your screen's layout. Something like this:

<com.example.stackoverflow.MyView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="17dp"
    ...
    />
like image 156
Ben P. Avatar answered Nov 08 '22 18:11

Ben P.