3

我想创建一个布局来浏览,看起来像类别和子类别:动态嵌套相对布局

enter image description here

这是分类,我在布局使这个由写.axml那看起来不错。现在,当用户点击类别相同的外观应该在其点击产生的,它是动态创建,但子内容重叠于母公司的内容,看起来像这样:

enter image description here

为了让孩子相对布局我写了下面代码:

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); 

请让我知道我应该改变,以使我的用户界面正确。 Thankyou

+0

是的亲爱的,我也通过改变布局来实现这一目标。当我使用嵌套线性布局来代替相对和放置文本和图像到一个更多的线性布局水平时,整个设计得到了正确。你在想那个吗? –

回答

2

您应该尝试Expandable ListView。我强烈建议使用它,因为它是内置控制,它具有与此相同的功能,如果您使用RelativeLayout,则必须管理许多场景,例如分辨率,内部项目,事件处理;在使用Expandable ListView时,您只需传递项目层次结构并完成UI。

+0

是的,我检查过它,但我不允许使用第三方软件包。任何人都可以建议我纠正我的代码中的任何内容吗? –

+0

没问题,但这不是第三方;它是一个像普通ListView一样的Android本机控件。 – RIYAZ

+0

它将适用于N级层次结构?我的代码支持N级层次结构,运行良好。但它适用于嵌套线性布局。我需要添加图像来指示关闭或开放的层次结构。所以,我用我的文字拍摄了一张图片,并将其转换为Relative,并陷入了这种情况 –