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

4
我希望创建一个类别和子类别浏览的布局,如下图所示: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 );

请告诉我应该如何更改才能使我的用户界面正确。谢谢。


是的亲爱的,我也通过更改布局实现了这一点。当我使用嵌套线性布局代替相对布局,并将文本和图像放置在另一个水平线性布局中时,整个设计就正确了。你也是这样想的吗? - Ajay Sharma
1个回答

3
你应该尝试使用 可扩展ListView。我强烈推荐使用这个控件,因为它是内置控件,并且具有你所需的相同功能。此外,如果你选择使用RelativeLayout,你需要处理许多情况,例如分辨率、内部项目和事件处理;而使用Expandable ListView,你只需传递项目层次结构,就可以完成UI了。

是的,我已经审核过了,但我不允许使用第三方包。有人能建议我在我的代码中纠正什么吗? - Ajay Sharma
没有问题,但这不是第三方控件;它是像普通的ListView一样的Android原生控件。 - Mohammad Riyaz
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Ajay Sharma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接