首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Xamarin Android中的动态嵌套相对布局

Xamarin Android中的动态嵌套相对布局
EN

Stack Overflow用户
提问于 2015-10-19 18:23:33
回答 1查看 1.1K关注 0票数 4

我想创建一个布局来浏览类别和子类别,如下所示:

这是针对类别的,我通过在layout中编写.axml来实现这一点。现在,当用户点击类别时,应该会在点击时生成相同的外观,并且它是动态创建的,但是子内容与父内容重叠,看起来像这样:

为了创建子相对布局,我编写了以下代码:

代码语言:javascript
复制
RelativeLayout childTaxonomylayout = new RelativeLayout( this );
childTaxonomylayout.Id = Convert.ToInt32( data.Value.Id );
childTaxonomylayout.SetOnClickListener( this );
var param = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent );
param.AddRule(LayoutRules.AlignParentBottom);
param.AddRule( LayoutRules.AlignParentLeft );
childTaxonomylayout.LayoutParameters = param;


TextView textViewChild = new TextView( this );
textViewChild.Text = data.Value.Item.Name;
textViewChild.SetPadding( 20, 10, 0, 10 );
textViewChild.Id = Convert.ToInt32( data.Value.Id );

RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.FillParent, RelativeLayout.LayoutParams.WrapContent );
rlp2.AddRule( LayoutRules.AlignParentBottom );
rlp2.AddRule( LayoutRules.AlignParentLeft );
textViewChild.LayoutParameters = rlp2;

childTaxonomylayout.AddView( textViewChild );
if (data.Value.Children.Count != 0) {
    var _imgViewClose = new ImageView( this );
    _imgViewClose.Id = Convert.ToInt32( "123" + data.Value.Id.ToString() );
    _imgViewClose.SetImageResource( Resource.Drawable.ic_right );

    RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(30, 20 );
    rlp1.AddRule( LayoutRules.AlignParentBottom );
    rlp1.AddRule( LayoutRules.AlignParentRight );
    childTaxonomylayout.AddView( _imgViewClose );
}

childLayout.AddView( childTaxonomylayout );

请让我知道我应该改变什么,以使我的UI正确。谢谢你

EN

回答 1

Stack Overflow用户

发布于 2015-10-19 22:11:50

你应该试试Expandable ListView。我强烈推荐使用它,因为它是内置控件,并且它具有你需要的相同功能。如果你使用RelativeLayout,你必须管理许多场景,例如解析,项目内部,事件处理;而使用Expandable ListView,你只需要传递项目层次结构,并使用UI完成。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33212014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档