It looks like a kind of wrong approach, but I’d still ask.
The task is, you have a layout xml that describes a composite widget (like Button + TextView). You’d like to make it re-usable, so you build a class like MyTextViewButtonWidget – it will expose its button text accessors and it will also do the same for text view:
public class MyTextViewButtonWidget extends LinearLayout {
...
void setButtonText(String text) { ... }
String getButtonText() { ... }
void setTextViewText(String text) { ... }
String getTextViewText() { ... }
...
}
Layout definition looks like this:
<LinearLayout ..........>
....button and text label here...
</LinearLayout>
The question is – how would you load this layout so that its root LinearLayout would be the LinearLayout part of MyTextViewButtonWidget?
Tried defining MyTextViewButtonWidget‘s ctor like this:
{
inflate(getContext(), R.layout.reusable_widget_layout, this);
}
But this loads reusable_widget_layout as a child to MyTextViewButtonWidget (that’s not what I need).
Generally, the problem is:
- You need to create a composite widget
- You’d like to be able to define its layout with xml markup
- You’d like it to load root’s child widgets defined in xml as its child widgets (INSTEAD OF: load the whole hierarchy from xml as a single child of your reusable widget)
Replace:
with: