2012-03-03 43 views
0

这里我的代码:addView()来inflateLayout

 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); 

current_play.xml

<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"/> 

</LinearLayout> 

R.id.llPlay(有XML显示左右)

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dip"> 

     <LinearLayout 
      android:id="@+id/llPlay" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" > 

     </LinearLayout> 

    </ScrollView> 

</LinearLayout> 

这里是onOptionsItemSelected()

 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不显示.. 为什么?我的错误是什么?

回答

0

你的问题是不明确的,所以我会猜一个答案:

在你onOptionsItemSelected()您搜索LinearLayout与ID R.id.llPlay并从它的所有子视图与removeAllViews()然后膨胀R.layout.current_play和纵横布局使LinearLayoutaktuelle_spiele)与Button,而且还有TextView(为此设置文本“Hallo”)。问题是,当你膨胀一个布局时,你只会膨胀那个布局上的视图。如果你希望你的布局也包含您在代码构建TextView那么你有2种选择:

1添加TextView直接在current_play.xml布局,并添加一个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> 

编辑:

2然后动态地在要建立布局,而是保持对这个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)比我想要显示的布局,我已经createt bevor .. – StefMa 2012-03-03 15:29:04

+0

@IceClaw然后选项2是为您。我已经更新了我的答案。 – Luksprog 2012-03-03 15:37:31

0

你忘了布局PARAMS为那个textview。

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

然后同时加入

ll.addView(tv);