9

イメージビューとテキストフィールドを備えた次のレイアウトがあります。

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="26dp"
        android:layout_marginTop="22dp"
        android:src="@drawable/a01" />

     <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="31dp"
        />

</RelativeLayout>

このレイアウトは透明になり、特定のアクティビティの上にこのレイアウトを呼び出したいのですが、特定のアクティビティが最初に開始されたときに、どのように実装しaddview()ますか?

4

3 に答える 3

44

表示したい場合:

FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
View.inflate(this, R.layout.overlay_layout, rootLayout);

次に、それを削除する場合:

FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
rootLayout.removeViewAt(rootLayout.getChildCount()-1);

これは簡潔な解決策です。XML ファイルで id を指定して を削除し、次に で削除するView必要があります。RelativeLayoutrootLayout.removeView(findViewById(R.id.the_id_of_the_relative_layout));

于 2013-01-07T12:02:19.980 に答える
2

ビューを追加するには、次のコードを使用してください。

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);

// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");

// insert into main view
View insertPoint =(View) findViewById(R.id.insert_point); // edited. 
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
于 2013-01-07T11:35:02.487 に答える
1

呼び出しアクティビティのレイアウトは FrameLayout 内にある必要があります (このレイアウトでは、最後に追加されたビューは常に前のビューの上に表示されます)。呼び出しアクティビティの onCreate メソッドで、LayoutInflater を使用して指定されたレイアウトを膨張させ、アクティビティの addView メソッドを直接使用します。

于 2013-01-07T11:45:15.937 に答える