0

Here my Code:

        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 (there xml shows so)

<?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>

Here is the 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;

If i click on my menu item, all my current views on R.id.llPlays are removed! The button from current_play is shown. But the TextView from "Here my code" dont show.. Why? Whre is my error?

4

2 回答 2

0

你的问题是模棱两可的,所以我猜一个答案:

在您的onOptionsItemSelected()搜索中,您LinearLayout使用 id搜索R.id.llPlay并从中删除所有子视图,removeAllViews()然后R.layout.current_play将布局扩展为具有LinearLayout( aktuelle_spiele) 和( )Button 以及TextView(您为其设置文本“Hallo”)。问题在于,当您对布局进行膨胀时,您只会对当时位于该布局上的视图进行膨胀。如果您希望您的布局也包含TextView您在代码中构建的内容,那么您有 2 个选项:

1TextView直接在current_play.xml布局中添加 id 并为其添加一个 id,以便您以后可以检索它TextView(whith 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-03T15:21:05.213 回答
0

您忘记了该 textview 的布局参数。

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

然后在添加时

ll.addView(tv);
于 2012-03-03T15:26:08.760 回答