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,並陷入了這種情況 –