利用Dialog,PopupWindow都可以实现对话框的功能。
Dialog实现比较方便,可以用系统自带的样式,也可以自定义一下,但是它显示位置比较固定,有时不能满足我们的需求。
而PopupWindow可以通过showAtLocation设置显示位置,也可以通过ShowAsDropDown以下拉方式显示在某个View的相对位置上,基本能满足我们的需求。
举个例子简单说明一下
为要使用自定义样式的dialog,需要写个style,在values/styles.xml里编写<style name="MyDialog" parent="@android:Theme.Dialog">
<!-- 设置dialog有无标题栏 -->
<item name="android:windowNoTitle">true</item>
<!-- 设置dialog出现时是否屏幕变黑 -->
<item name="android:backgroundDimEnabled">false</item>
<!-- 设置window的背景 -->
<item name="android:windowBackground">@null</item>
<!-- Dialog的windowFrame框为无 -->
<item name="android:windowFrame">@null</item>
<!-- 是否浮现在activity之上 -->
<item name="android:windowIsFloating">false</item>
<!-- 是否半透明 -->
<item name="android:windowIsTranslucent">false</item>
<!-- 设置dialog的背景 -->
<item name="android:background">@android:color/background_dark</item>
</style>
调用的时候这样调用
AlertDialog.Builder aBuilder = new AlertDialog.Builder(this,
R.style.MyDialog);
RelativeLayout layout = (RelativeLayout) getLayoutInflater()
.inflate(R.layout.activity_main_custom, null);
aBuilder.setView(layout);
AlertDialog dialog = aBuilder.create();
dialog.show();
再说下使用Activity作为对话框的用法:
定义一个style
<style name="PopupActivity">
<!-- 设置window的背景 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 是否半透明 -->
<item name="android:windowIsTranslucent">true</item>
<!-- 设置dialog有无标题栏 -->
<item name="android:windowNoTitle">true</item>
<!--设置切换动画 -->
<item name="android:windowAnimationStyle">@style/Anim_scale</item>
</style>
动画效果
<style name="Anim_scale" parent="@android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">@anim/scale_in</item>
<item name="android:activityOpenExitAnimation">@anim/scale_out</item>
<item name="android:activityCloseEnterAnimation">@anim/scale_in</item>
<item name="android:activityCloseExitAnimation">@anim/scale_out</item>
</style>
最后看下MainAcivity.java类
public class MainActivity extends Activity implements OnClickListener {
private Button mButtonDefaultDialog;
private Button mButtonCustomDialog;
private Button mButtonPopupWindow;
private Button mButtonPopupActivity;
PopupWindow popupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mButtonDefaultDialog = (Button) findViewById(R.id.btn_main_default_dialog);
mButtonCustomDialog = (Button) findViewById(R.id.btn_main_custom_dialog);
mButtonPopupWindow = (Button) findViewById(R.id.btn_main_popup_window);
mButtonPopupActivity = (Button) findViewById(R.id.btn_main_popup_activity);
mButtonDefaultDialog.setOnClickListener(this);
mButtonCustomDialog.setOnClickListener(this);
mButtonPopupWindow.setOnClickListener(this);
mButtonPopupActivity.setOnClickListener(this);
// 装载R.layout.popupwindow对应的布局
View root = getLayoutInflater().inflate(R.layout.popupwindow, null);
// 创建PopupWindow对象
popupWindow = new PopupWindow(root, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
// 实例化popupwindow.xml布局中的组件
Button button = (Button) root.findViewById(R.id.close_popup_window);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_main_default_dialog:
// 使用系统自带theme
AlertDialog.Builder builder = new AlertDialog.Builder(this,
R.style.AppTheme);
RelativeLayout view = (RelativeLayout) getLayoutInflater().inflate(
R.layout.activity_main_default, null);
builder.setView(view);
AlertDialog alertDialog = builder.create();
alertDialog.show();
break;
case R.id.btn_main_custom_dialog:
// 直接使用Dialog的方式已经不推荐使用
// Dialog dialog = new Dialog(MainActivity.this, R.style.MyDialog);
// dialog.setContentView(R.layout.activity_main_custom);
// dialog.show();
// 使用自定义theme
AlertDialog.Builder aBuilder = new AlertDialog.Builder(this,
R.style.MyDialog);
RelativeLayout layout = (RelativeLayout) getLayoutInflater()
.inflate(R.layout.activity_main_custom, null);
aBuilder.setView(layout);
AlertDialog dialog = aBuilder.create();
dialog.show();
break;
case R.id.btn_main_popup_window:
// 以下拉方式显示
// popupWindow.showAsDropDown(arg0);
// 将PopupWindow显示在指定位置
popupWindow.showAtLocation(mButtonPopupWindow, Gravity.BOTTOM,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
break;
case R.id.btn_main_popup_activity:
// 弹出式显示Activity
Intent intent = new Intent(MainActivity.this, PopupActivity.class);
startActivity(intent);
break;
case R.id.close_popup_window:
// 关闭PopupWindow
popupWindow.dismiss();
break;
default:
break;
}
}
}