Android--将Activity设置成Dialog

jopen 12年前

Android--将Activity设置成Dialog

package com.mrzhu.dialogtest;    import android.app.Activity;  import android.content.Intent;  import android.os.Bundle;  import android.view.View;    public class DialogTestActivity extends Activity {      /** Called when the activity is first created. */      @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);      }            public void click(View v){       startActivity(new Intent(DialogTestActivity.this, Activity2.class));      }  }

 

package com.mrzhu.dialogtest;    import android.app.Activity;  import android.os.Bundle;  import android.view.Window;  import android.view.WindowManager;  import android.view.WindowManager.LayoutParams;    public class Activity2 extends Activity {   @Override   protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity2);    Window window = getWindow();    WindowManager.LayoutParams layoutParams = window.getAttributes();    //设置窗口的大小及透明度    layoutParams.width = LayoutParams.FILL_PARENT;    layoutParams.height = layoutParams.FILL_PARENT;    layoutParams.alpha = 0.5f;    window.setAttributes(layoutParams);   }  }

 

AndroidManifest.xml中加入:

 <activity              android:name=".Activity2"              android:theme="@style/Transparent" >          </activity>

 

style.xml:

<?xml version="1.0" encoding="utf-8"?>  <resources>        <style name="Transparent" parent="android:style/Theme.Dialog">          <item name="android:windowNoTitle">true</item>          <item name="android:windowIsTranslucent">true</item>          <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>      </style>    </resources>

 

main.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" >        <Button          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:onClick="click"          android:text="click" />    </LinearLayout>