本文共 4515 字,大约阅读时间需要 15 分钟。
利用Dialog,PopupWindow都可以实现对话框的功能。
Dialog实现比较方便,可以用系统自带的样式,也可以自定义一下,但是它显示位置比较固定,有时不能满足我们的需求。
而PopupWindow可以通过showAtLocation设置显示位置,也可以通过ShowAsDropDown以下拉方式显示在某个View的相对位置上,基本能满足我们的需求。
举个例子简单说明一下
为要使用自定义样式的dialog,需要写个style,在values/styles.xml里编写 <style name="MyDialog" parent="@android:Theme.Dialog"> <item name="android:windowNoTitle">true</item> <!-- 设置dialog出现时是否屏幕变黑 --> <item name="android:backgroundDimEnabled">false</item> <item name="android:windowBackground">@null</item> <!-- Dialog的windowFrame框为无 --> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">false</item> <item name="android:windowIsTranslucent">false</item> <item name="android:background">@android:color/background_dark</item> AlertDialog.Builder aBuilder = new AlertDialog.Builder(this, RelativeLayout layout = (RelativeLayout) getLayoutInflater() .inflate(R.layout.activity_main_custom, null); aBuilder.setView(layout); AlertDialog dialog = aBuilder.create(); <style name="PopupActivity"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:windowAnimationStyle">@style/Anim_scale</item> <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> public class MainActivity extends Activity implements OnClickListener { private Button mButtonDefaultDialog; private Button mButtonCustomDialog; private Button mButtonPopupWindow; private Button mButtonPopupActivity; 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 = new PopupWindow(root, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); // 实例化popupwindow.xml布局中的组件 Button button = (Button) root.findViewById(R.id.close_popup_window); button.setOnClickListener(this); public void onClick(View v) { case R.id.btn_main_default_dialog: AlertDialog.Builder builder = new AlertDialog.Builder(this, RelativeLayout view = (RelativeLayout) getLayoutInflater().inflate( R.layout.activity_main_default, null); AlertDialog alertDialog = builder.create(); case R.id.btn_main_custom_dialog: // Dialog dialog = new Dialog(MainActivity.this, R.style.MyDialog); // dialog.setContentView(R.layout.activity_main_custom); AlertDialog.Builder aBuilder = new AlertDialog.Builder(this, RelativeLayout layout = (RelativeLayout) getLayoutInflater() .inflate(R.layout.activity_main_custom, null); aBuilder.setView(layout); AlertDialog dialog = aBuilder.create(); case R.id.btn_main_popup_window: // popupWindow.showAsDropDown(arg0); popupWindow.showAtLocation(mButtonPopupWindow, Gravity.BOTTOM, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); case R.id.btn_main_popup_activity: Intent intent = new Intent(MainActivity.this, PopupActivity.class); case R.id.close_popup_window: 最终效果如下: 参考: