How to save layout attributes when merging a custom Android view?

I am trying to replace a set of views with a custom composite view that should do the same. In particular, I often repeat the following layout:

<LinearLayout style="@style/customLayoutStyle"> <Button style="@style/customButtonStyle" /> <TextView style="@style/customTextViewStyle" /> </LinearLayout> 

My goal is to replace this block with one <Highlighter /> .

For this purpose, I define something like res / layout / highlighter.xml

 <merge xmlns:android="http://schemas.android.com/apk/res/android" style="@style/customLayoutStyle"> <Button android:id="@+id/btnHighlighter" style="@style/customButtonStyle" /> <TextView android:id="@+id/lblHighlighter" style="@style/customTextViewStyle" /> </merge> 

And in my usual view, I have something like

 public class Highlighter extends LinearLayout { public Highlighter(Context context, AttributeSet attrs) { super(context, attrs); inflate(context, R.layout.highlighter, this); } } 

This basically works, but some layout options for the <merge> ignored. This screenshot shows what seems to be wrong. The 3 images on the bottom line are aligned correctly using the 3x LinearLayout block that I am trying to replace. Only the top left image uses a custom view. I assume that the layout options for padding and layout_weight are lost. Am I doing something wrong, or do I need a workaround?

+4
source share
1 answer

You are right about the lost parameters. To get around this, you can put the style definition for Highlighter in the layout where you define Highlighter.

eg.

 <yournamespace.Highlighter style="@style/customLayoutStyle" /> 
+3
source

All Articles