0

ここに私のコード:

        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 は表示されません。なぜですか? 私のエラーはどこですか?

4

2 に答える 2

0

あなたの質問はあいまいなので、私は答えを推測します:

idでをonOptionsItemSelected()検索し、そこからすべての子ビューを削除してから、レイアウトを膨張させて( ) だけでなく(テキスト "Hallo" を設定する) を持つようにします。問題は、レイアウトをインフレートするときに、その時点でそのレイアウトにあるビューのみをインフレートすることです。レイアウトにコードで作成したものも含めたい場合は、次の 2 つのオプションがあります。LinearLayoutR.id.llPlayremoveAllViews()R.layout.current_playLinearLayoutaktuelle_spieleButtonTextViewTextView

1 をレイアウトにTextView直接current_play.xml追加し、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

そのテキストビューのレイアウト パラメータを忘れました。

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

それから追加しながら

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