c# - Android custom viewgroup not rendering children -
i trying write custom viewgroup duplicate lot across layout files. simple one, 2 textboxes placed within linearlayout vertical orientation , padding. worth mentioning use xamarin develop application, don't think issue xamarin specific. implementation follows:
[register("qube.applistitem")] class applistitem : linearlayout { private string m_label; private string m_value; private int m_labelsize; private int m_valuesize; private int m_horizontalpadding; private int m_verticalpadding; private valuetype m_valuetype; private textview m_labelview; private textview m_valueview; public applistitem(context context) : this(context, null) { } public applistitem(context context, iattributeset attrs) : this(context, attrs, 0) { } public applistitem(context context, iattributeset attrs, int defstyleattr) : base(context, attrs, defstyleattr) { initialize(context, attrs); } private void initialize(context context, iattributeset attrs) { var = context.obtainstyledattributes(attrs, resource.styleable.applistitem, 0, 0); m_label = a.getstring(resource.styleable.applistitem_label); m_value = a.getstring(resource.styleable.applistitem_value); m_labelsize = a.getdimensionpixeloffset(resource.styleable.applistitem_labelsize, resource.dimension.font_small); m_valuesize = a.getdimensionpixeloffset(resource.styleable.applistitem_valuesize, resource.dimension.font_medium); m_horizontalpadding = a.getdimensionpixeloffset(resource.styleable.applistitem_horizontalpadding, resource.dimension.row_padding); m_verticalpadding = a.getdimensionpixeloffset(resource.styleable.applistitem_verticalpadding, resource.dimension.info_list_padding); m_valuetype = (valuetype)a.getint(resource.styleable.applistitem_valuetype, (int)valuetype.string); a.recycle(); orientation = orientation.vertical; clickable = true; setpadding(m_horizontalpadding, m_verticalpadding, m_horizontalpadding, m_verticalpadding); if (build.version.sdkint >= buildversioncodes.honeycomb) { // if we're running on honeycomb or newer, can use theme's // selectableitembackground ensure view has pressed state typedvalue outvalue = new typedvalue(); context.theme.resolveattribute(android.resource.attribute.selectableitembackground, outvalue, true); setbackgroundresource(outvalue.resourceid); } buildview(); } private void buildview() { m_labelview = new textview(context); m_labelview.layoutparameters = new viewgroup.layoutparams(viewgroup.layoutparams.matchparent, viewgroup.layoutparams.wrapcontent); m_labelview.textsize = m_labelsize; m_labelview.text = m_label; addview(m_labelview); if (m_valuetype == valuetype.edittext) m_valueview = new edittext(context); else m_valueview = new textview(context); m_valueview.layoutparameters = new viewgroup.layoutparams(viewgroup.layoutparams.matchparent, viewgroup.layoutparams.wrapcontent); m_valueview.textsize = m_valuesize; m_valueview.text = m_value; addview(m_valueview); } protected override void onmeasure(int widthmeasurespec, int heightmeasurespec) { base.onmeasure(widthmeasurespec, heightmeasurespec); } protected override void onlayout(bool changed, int l, int t, int r, int b) { base.onlayout(changed, l, t, r, b); } public string label { { return m_label; } set { m_label = value; m_labelview.text = value; invalidate(); requestlayout(); } } public string value { { return m_valueview.text; } set { m_valueview.text = value; m_value = value; invalidate(); requestlayout(); } } public enum valuetype { string, edittext } } the expected behavior is, said, 2 textboxes stacked on top of each other, however, actual behavior view height 0 (if android:layout_height="wrap_content"). setting layout_height 20dp manually shows blank view of height.
okay, mistake made silly one, expected. when dimensions typedarray, default value pass in resource id's instead of actual values associated id's. example:
m_labelsize = a.getdimensionpixeloffset(resource.styleable.applistitem_labelsize, resource.dimension.font_small); should be
m_labelsize = a.getdimensionpixeloffset(resource.styleable.applistitem_labelsize, resources.getdimensionpixeloffset(resource.dimension.font_small));
Comments
Post a Comment