java - Create custom layout in XML to initialize in code or in another layout xml -
i have encountered issue trying resolve (or understand better way should done) in creation of custom layout in android.
i want create custom relativelayout
class use, defined in layout xml file.
my_relative_layout.xml
<?xml version="1.0" encoding="utf-8"?> <com.mypackage.myrelativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <imageview android:id="@+id/image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" android:scaletype="fitstart" android:src="@drawable/my_drawable"/> <textview android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="@color/title_gray" android:layout_below="@id/image_view" android:gravity="center" android:text="@string/placeholder" /> </com.mypackage.myrelativelayout>
usage
public class myrelativelayout extends relativelayout { private attributeset attrs; private imageview imageview; private textview textview; public myrelativelayout(context context) { super(context); } public myrelativelayout(context context, attributeset attrs) { super(context, attrs); this.attrs = attrs; } public myrelativelayout(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); this.attrs = attrs; } @override protected void onfinishinflate() { super.onfinishinflate(); if (attrs != null) { typedarray = getcontext().gettheme().obtainstyledattributes(attrs, r.styleable.myrelativelayout, 0, 0); drawableresource = a.getresourceid(r.styleable.myrelativelayout.image_view, 0); a.recycle(); } imageview = (imageview) findviewbyid(r.id.image_view); textview = (textview) findviewbyid(r.id.text_view); if (drawableresource != 0 && imageview != null) { imageview.setimageresource(drawableresource); } } }
my issue want initialise layout both in xml , in code. wrote class , xml, can use in code doing:
mylayout = (myrelativelayout) layoutinflater.from(this.getactivity()).inflate(r.layout.my_relative_layout, container, false);
when writing following in xml causes onfinishinflate fail on getviewbyid (returns null) , childrencount 0
<com.mypackage.myrelativelayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_below="@id/another_layout" app:image_view="@drawable/my_image" />
and doing following, won't let me configure custom image_view attribute.
<include layout="@layout/my_relative_layout"/>
to fix that, can change custom layout xml root element of type relativelayout , add following beginning of onfinishinflate method:
layoutinflater.from(getcontext()).inflate(r.layout.my_relative_layout, this, true);
but xml won't reference class.
questions are,
1. missing in definition of custom layout?
2. correct definition custom layout?
thank in advance!
first should use styled attributes in contractor shown in this example.
how can expect myrelativelayout defined fallow:
<com.mypackage.myrelativelayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_below="@id/another_layout" app:image_view="@drawable/my_image" />
to aware of views defined in my_relative_layout.xml
?
to make work should create costume layout , add com.mypackage.myrelativelayout
manually.
some thing that:
costume_layout.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <imageview android:id="@+id/image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" android:scaletype="fitstart" android:src="@drawable/my_drawable"/> <textview android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="@color/title_gray" android:layout_below="@id/image_view" android:gravity="center" android:text="@string/placeholder" /> </relativelayout>
your costume view
public class myrelativelayout extends relativelayout { private attributeset attrs; private imageview imageview; private textview textview; public myrelativelayout(context context) { super(context); init(); } public myrelativelayout(context context, attributeset attrs) { super(context, attrs); init(); } public myrelativelayout(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); init(); } private void init(context context){ layoutinflater.from(context).inflate(r.layout.costume_layout, this); } }
Comments
Post a Comment