2012-03-03 4 views
0

을 inflateLayout하기 onOptionsItemSelected()addView() 여기 내 코드

 case R.id.current_play: 
      LinearLayout mainLayout = (LinearLayout) findViewById(R.id.llPlay); 
      mainLayout.removeAllViews(); 
      LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      inflater.inflate(R.layout.current_play, mainLayout, true); 
      return true; 

내 메뉴 항목을 클릭하면 R.id.llPlays의 모든 현재보기가 제거됩니다! current_play의 버튼이 표시됩니다. 하지만 Textview "Here my code"는 표시되지 않습니다 .. 왜? Whre가 내 오류 야?

답변

0

귀하의 질문은 내가 대답을 생각합니다 모호 :에서

당신의 onOptionsItemSelected() 당신은 ID R.id.llPlay으로 LinearLayout를 검색하고 removeAllViews()으로 모든 아이 뷰를 제거한 다음 R.layout.current_play와 화면 레이아웃을 팽창 Button과 함께 LinearLayout (aktuelle_spiele)을 가지며 TextView (텍스트로 "Hallo"로 설정)도 있습니다. 문제는 레이아웃을 부 풀릴 때 그 순간에 그 레이아웃에있는 뷰만 팽창 시킨다는 것입니다. 당신이 당신의 레이아웃을 원하는 경우 또한 코드에서 구축 TextView는 다음 두 가지 옵션이 있습니다 것을 포함합니다 : 그래서 당신이 나중에 검색 할 수 있습니다

1 직접 current_play.xml 레이아웃에 TextView를 추가하고 또한에 ID를 추가를 TextView (아 파크 - 그 findViewById(R.id.the_text)) 및 텍스트에 "안녕"로 설정 :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/aktuelle_spiele" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 
    <TextView 
     android:id="@+id/the_text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" />  
</LinearLayout> 

편집 :

이 동적으로 원하는 곳 레이아웃을 구축하지만 inflat에 대한 참조를 길게 에드 뷰.

LinearLayout ll = (LinearLayout) View.inflate(TabAndPlay.this, R.layout.current_play, null); 
     TextView tv = new TextView(TabAndPlay.this); 
     tv.setText("hallo"); 
     tv.setBackgroundColor(Color.GRAY); 
     ll.addView(tv); 

가 그 부풀려 ll에 대한 참조를 유지하고 기본 레이아웃에 onOptionsItemSelected() 콜백에 전달 :이 같은 뷰 생성한다면,

case R.id.current_play: 
      LinearLayout mainLayout = (LinearLayout) findViewById(R.id.llPlay); 
      mainLayout.removeAllViews(); 
      mainLayout.add(ll); 
      return true; 
+0

감사를 추가하는 동안,하지만 직접 current_play에 내가 동적 textviews를 얻을 수 있기 때문에 얻을니까. 하나 이상의 레이아웃을 동적으로 (디스플레이가 아닌) 만들고 사용자가 메뉴 (R.id.current_play)를 선택하는 것보다 레이아웃을 표시하고 싶습니다 ... – StefMa

+0

@IceClaw 다음 옵션 2는 당신. 내 대답을 업데이트했습니다. – Luksprog

0

을 당신은 레이아웃 PARAMS를 잊어 버렸습니다 그 textview.

LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(wrap_content,wrap_content); 

그런

ll.addView(tv);