下面是我的代码:
        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的按钮。但是"Here my code“中的TextView没有显示..为什么?我的错误在哪里?
发布于 2012-03-03 23:21:05
你的问题模棱两可,所以我来猜猜答案:
在您的onOptionsItemSelected()中,搜索id为R.id.llPlay的LinearLayout,并使用removeAllViews()删除其中的所有子视图,然后扩展R.layout.current_play并对您的布局进行分析,以使LinearLayout (aktuelle_spiele)包含Button以及TextView(您为其设置了文本"Hallo")。问题是,当您膨胀一个布局时,您只膨胀了当时在该布局上的视图。如果您希望布局还包含您在代码中构建TextView,那么您有两个选择:
1直接在current_play.xml布局中添加TextView,并向其添加id,以便稍后可以检索该TextView(使用findViewById(R.id.the_text))并将文本设置为"Hallo":
<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然后在你想要的地方动态构建布局,但是保持对那个膨胀视图的引用。因此,如果您像这样创建一个视图:
 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;发布于 2012-03-03 23:26:09
您忘记了该文本视图的布局参数。
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(wrap_content,wrap_content);然后在添加时
ll.addView(tv);https://stackoverflow.com/questions/9546981
复制相似问题