2013-07-10 57 views
1

是否有可能創建一個方法來擴充佈局(或動態創建它),添加特定的視圖(在本例中爲TextViews),然後將佈局重新設置爲視圖,以便我可以(以某種方式)把它嵌入到其他類的主佈局中,比如嵌套,但是動態添加元素?LinearLayout嵌套

回答

1

是有可能:

... onCreate() 
    { 
    setContentView(...); 
    ViewGroup myContainer=(ViewGroup)findViewById(R.id.myContainer); 
    View v=inflateMySpecialView(); 
    //=<set layoutParams for the view if needed 
    myContainer.addView(v); 
    } 

public View inflateMySpecialView() 
    { 
    ViewGroup viewgroup=(ViewGroup) getLayoutInflater().inflate(R.layout.my_custom_layout, null,false); 
    //do some stuff with the inflated viewgroup. 
    return viewgroup; 
    } 
3

是的,您可以使用LayoutInflater類來擴充現有佈局。它會去是這樣的:

public static View GetLayout(final Context c){ 
    final LayoutInflater inflater = LayoutInflater.from(c); 
    final View v = inflater.inflate(R.layout.activity_main, null); 

    //find the container where you want to insert your dynamic items 
    final LinearLayout placeholder = (LinearLayout) v.findViewById(R.id.linearLayout1); 

    //create the new textview 
    final TextView text = new TextView(c); 
    text.setText("Some text"); 

    placeholder.addView(text, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 

    return v; 

} 

的問題問,這種觀點被返回到「其他類」,而其他的答案是從同一類這樣做 - 這意味着它有一個背景。爲了從另一個類中這樣做,你需要傳遞一個上下文,就像我在這裏做的那樣,或者其他的方式(如果你想要一個實例對象,就要調用構造器)。

+0

每個ViewGroup都有它自己的LayoutParams類。確保你已經導入了正確的佈局參數(或者當你創建它們的時候指定包) - 並且記住,爲父項輸入了參數,而不是被添加的項目。 –

+0

我的進口很好,但錯誤在這裏。 –

+0

將整個源代碼粘貼到您的pastebin中。 –

1

是的,它是。我在我的應用程序中做了類似的事情。我正在寫一個flashcard應用程序,並使用scrollview向用戶顯示他們創建的所有套牌。該代碼是說:

public void FillDeckView(){ 
     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     Deck testDeck = Deck.GetDeckFromDB();//Make a deck object from SQLite DB values 

     final int DECK_SIZE = 20;//Fill the view with this many decks 

     TableLayout scrollTable = (TableLayout) findViewById(R.id.scrollTable); 
         //This tableRow is in my main view 
     TableRow newTableRow = (TableRow) findViewById(R.id.tableRow4); 
         //This view I'm inflating is a separate android layout XML file 
         //I created, which shows the icon for the deck the user has made. 
     View deckViewBox = inflater.inflate(R.layout.deck_icons, null); 
         //Look, I'm getting a textview that's IN the deckViewBox, which is 
         //a separate View. Below, I'll change its text dynamically 
     TextView deckNameTV = (TextView) deckViewBox.findViewById(R.id.deckNameView); 

     int viewIndex = 1; 
     for(int i = 0; i < DECK_SIZE && testDeck != null; i++){ 
          //First I'll change the text of the view, and then I'll add it in 
      deckNameTV.setText(testDeck.getName()); 
      newTableRow.addView(deckViewBox); 
      if(i % 2 != 0){ 
       //If we're on an odd number, make a new tableRow 
       newTableRow = new TableRow(getApplicationContext()); 
       scrollTable.addView(newTableRow, viewIndex); 
       ++viewIndex; 
      }   
     } 
}//FillDeckView 

基本上,你需要從充氣一個新的佈局視圖,然後調用findViewById()爲你創建新的視圖的方法。從那裏,就像操縱用戶可以看到的當前視圖一樣。你可以從那裏做任何你想做的事。

相關問題