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!
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 View
s 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"
...
/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With