这里我的代码: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不显示.. 为什么?我的错误是什么?
感谢,而是直接在current_play不明白,因为我得到的动态textviews。我喜欢动态创建一个或多个布局(这不是显示),如果用户选择菜单(R.id.current_play)比我想要显示的布局,我已经createt bevor .. – StefMa 2012-03-03 15:29:04
@IceClaw然后选项2是为您。我已经更新了我的答案。 – Luksprog 2012-03-03 15:37:31