android - Centered textview does not appear in RelativeLayout -
i have textview, need centered horizontally , vertically inside square. this, have put inside relativelayout:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:background="#fff" > <textview android:id="@+id/centered_text_view_text" android:layout_centerinparent="true" android:text="test" android:background="#00ff00" android:textcolor="#000" android:textsize="16dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </relativelayout>
i instantiate relativelayout layoutinflater , call layout method on relativelayout position need it.
layoutinflater inflater = helpers.getinflater(); containerlayout = (relativelayout) inflater.inflate(r.layout.centered_text_view, null); textview = (textview) containerlayout.findviewbyid(r.id.centered_text_view_text); ... (later in code) containerlayout.layout(dayleft, weektop, dayright, weekbottom);
my problem textview not visible. can see white background relativelayout, neither text nor background textview.
i have tried getting rid of relativelayout , calling layout method on textview. haven't been able work, however, because text horizontally, , not vertically, aligned after set gravity on textview.
first - custom views should extend sort of view.class
. class extend should whatever root view in xml
.
second - keeping reference containerlayout
(root view) redundant. class is root view.
replace containerlayout
this
or removing entirely:
textview = (textview) findviewbyid(r.id.centered_text_view_text);
third - can't how inflating wrong, have never done that.
instead of calling layout
on containerlayout
, try inflating inflate()
method. (you access when extending sub class of view
)
inflate(getcontext(), r.layout.view_user_tag, this);
notice how setting this
root, , using inflater view class.
your class may this:
public static class textinlinear extends relativelayout { public textview textview; //region default constructers public void textinlinear(context context){ this(context, null); } public void textinlinear(context context, attributeset attrs){ this(context, attrs, 0); } public void textinlinear(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); if(!isineditmode()) init(); } //endregion private void init(){ inflate(getcontext(), r.layout.view_user_tag, this); textview = (textview) this.findviewbyid(r.id.centered_text_view_text); } /* more methods. i.e. settext(), gettext() or whatever */ }
Comments
Post a Comment