레이아웃을 부 풀리거나 (동적으로 생성하는) 특정 뷰 (이 경우 TextViews)를 추가 한 다음 레이아웃을 뷰로 다시 설정하여 (어쨌든) 중첩과 같은 다른 클래스의 주 레이아웃에 통합하지만 요소를 동적으로 추가 할 수 있습니까?LinearLayout 중첩
1
A
답변
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;
}
질문이 뷰가 "다른 클래스"를 반환하고 다른 답변은 같은 클래스에서 그렇게하고 있다는 것을 질문 : 그것은 무언가 같이 갈 것입니다. 다른 클래스에서 그렇게하기 위해, 여기서하고있는 것처럼 컨텍스트를 전달하거나 다른 방법으로 (인스턴스 객체를 원한다면 생성자 호출 등) 전달해야합니다.
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를()를 호출 할 필요가있다. 거기에서 사용자가 볼 수있는 현재보기를 조작하는 것과 같습니다. 거기에서 원하는 모든 것을 할 수 있습니다.
관련 문제
- 1. 중첩 된 LinearLayout 사용
- 2. LinearLayout 내부에 중첩 된 RelativeLayout 내부의 ImageViews
- 3. linearlayout height matching other linearLayout
- 4. 있는 LinearLayout 수평있는 LinearLayout 수직
- 5. LinearLayout within 2 LinearLayout in 안드로이드
- 6. LinearLayout 아래에 다른 LinearLayout 아래에 RelativeLayout 놓기
- 7. 가있는 LinearLayout
- 8. 있는 LinearLayout
- 9. LinearLayout 중력
- 10. LinearLayout 확장
- 11. LinearLayout Id
- 12. LinearLayout layout_weight
- 13. LinearLayout 연장하기
- 14. LinearLayout Center
- 15. LinearLayout 가중치가 부모를 채우지 않음
- 16. LinearLayout (linearLayout)을 내 수업에서 어떻게 볼 수 있습니까?
- 17. Android LinearLayout Multi-Touch
- 18. LinearLayout 내부에 ImageView 추가
- 19. LinearLayout 반투명 (투명하지 않음)
- 20. linearlayout/textviews 스크롤 가능합니까?
- 21. ListView 아래에 LinearLayout 추가
- 22. android LinearLayout 디스플레이 문제
- 23. RelativeLayout 내지도에서 겹치는 LinearLayout
- 24. xml LinearLayout 다른 동작
- 25. 편집보기는 프로그램의 LinearLayout
- 26. Android LinearLayout 선택기 배경색
- 27. 의 LinearLayout 바로 상위
- 28. TextView 주위에 LinearLayout 래핑
- 29. 널 예외 오류가있는 LinearLayout
- 30. LinearLayout 공간 분포
모든 ViewGroup에는 고유 한 LayoutParams 클래스가 있습니다. 올바른 레이아웃 매개 변수를 가져 왔는지 (또는 만들 때 패키지를 지정했는지) 확인하십시오. 추가되는 항목이 아닌 상위 항목에 대한 매개 변수가 입력됩니다. –
내 수입은 괜찮지 만 여기에 오류가 있습니다. –
전체 소스를 pastebin에 붙여 넣으십시오. –