1

我正在創建自定義佈局(擴展FrameLayout)。我有一堆在xml中定義的視圖(現在沒關係)。
我需要做什麼。我的自定義佈局具有自定義的屬性,我們假設它名爲footer_banner_type
我有不同的橫幅班,其中一些我很不同,因此我不能在xml中放置一些基本橫幅。所以我必須添加一些基於屬性值的橫幅。
我在延長FrameLayout。我是新手,這是我的第一個自定義佈局。如何在運行時自定義佈局中添加視圖

我不知道如何提高性能。
據我瞭解佈局迭代和充氣所有子視圖。但是如果我需要在運行時添加視圖。我不想讓佈局重申視圖層次結構,因爲這會是性能問題。
我的問題是如何更好地實施我的任務。

回答

0
//First create your view: 
View wonderfulView = new View(this.getApplicationContext()); 
//Then, create it's LayoutParams 
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1); 
//Set those layout params in your view 
wonderfulView.setLayoutParams(params); 
//finaly, add the view to your ViewGroup 
yourLayout.addView(wonderfulView); 

就是這樣。

如果要更改視圖的容器,你必須將其刪除形成以前的母公司是這樣的:

View movingThing = ((YourLayoutClass)findViewById(R.id.currentContainer)).getChildAt(WhereYourViewis); 
((YourLayoutClass)findViewById(R.id.currentContainer)).removeView(movingThing); 
((YourLayoutClass)findViewById(R.id.newContainer)).addView(movingThing); 
相關問題