赞
踩
Android有三种方式实现对话框(Dialog,PopupWindow,Activity),这里记录一下Activity的方式。
在style.xml文件中自定义一个style
- <style name="DialogStyle">
- <!--设置dialog的背景-->
- <item name="android:windowBackground">@android:color/transparent</item>
- <!--设置Dialog的windowFrame框为无-->
- <item name="android:windowFrame">@null</item>
- <!--设置无标题-->
- <item name="android:windowNoTitle">true</item>
- <!--是否浮现在activity之上-->
- <item name="android:windowIsFloating">true</item>
- <!--是否半透明-->
- <item name="android:windowIsTranslucent">true</item>
- <!--设置窗口内容不覆盖-->
- <item name="android:windowContentOverlay">@null</item>
- <!--设置动画,在这里使用让它继承系统的Animation.Dialog-->
- <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
- <!--背景是否模糊显示-->
- <item name="android:backgroundDimEnabled">true</item>
- </style>
- public class InfoActivity extends Activity {
- Button btnPhone;
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- super.onCreate(savedInstanceState);
- setContentView(R.layout.dialog_info);
- setFinishOnTouchOutside(true);
-
- btnPhone = (Button) findViewById(R.id.btnPhone);
-
- btnPhone.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(getApplicationContext(), "已拨号", Toast.LENGTH_SHORT).show();
-
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_CALL);
- intent.setData(Uri.parse("tel:13866668888"));
- startActivity(intent);
- }
- });
- }
- }
值得注意的是如需要点击区域外消失则要下面这段代码
setFinishOnTouchOutside(true);
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:background="@color/white"
- android:layout_height="match_parent">
-
- <LinearLayout
- android:layout_width="match_parent"
- android:orientation="vertical"
- android:layout_height="100dp">
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:text="手机:"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:text="13866668888"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <Button
- android:id="@+id/btnPhone"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="拨打电话" />
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
在AndroidManifest注册Activity的时候声明theme是第一步所自定义的style。
- <activity android:name=".activity.InfoActivity"
- android:theme="@style/DialogStyle"/>
startActivity(new Intent(activity, InfoActivity.class));
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。