2013-07-10 2 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

네, 있습니다. 내 애플 리케이션에서 비슷한 일을. 플래시 카드 앱을 쓰고 있는데, 사용자가 만든 모든 데크를 보여주기 위해 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를()를 호출 할 필요가있다. 거기에서 사용자가 볼 수있는 현재보기를 조작하는 것과 같습니다. 거기에서 원하는 모든 것을 할 수 있습니다.