1)在需要設置成對話框的Activity在AndroidManifest.xml中配置
android:theme=”@android:style/Theme.Dialog”
2)PopWindow也可以做類似對話框風格的窗口
只需要兩步就可以完成:
a)創建PopWindow對象,爲其設置佈局內容與寬度、高度
b)調用pop.showAsDropDown(View)將PopupWindow作爲View組件以下拉組件顯示出來, 或者調用showAtLocation()方法將PopupWindow在指定位置顯示出來
第一個參數指定PopupWindow的錨點view,即依附在哪個view上。
第二個參數指定起始點。
第三,四個參數設置以起始點的右下角爲原點,向向右、下各偏移量。
package com.xspacing.popwindow;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
/**
* @ClassName MainActivity.java
* @Description TODO
* @author smile
* @version v1.0
* @date 2016年9月28日
*/
public class MainActivity extends Activity {
public PopupWindow mPopWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showPopWindow(View v) {
getPopWindow();
mPopWindow.showAsDropDown(v);
// 其他顯示PopWindow位置的方法
// mPopWindow.showAsDropDown(anchor, xoff, yoff);
// mPopWindow.showAsDropDown(anchor, xoff, yoff, gravity);
// mPopWindow.showAtLocation(v, Gravity.CENTER, 200, 200);
}
private void getPopWindow() {
if (mPopWindow != null) {
mPopWindow.dismiss(); // 關閉PopWindow
return;
} else {
initPopWindow();
}
}
@SuppressLint("ClickableViewAccessibility")
private void initPopWindow() {
View contentView = View.inflate(this, R.layout.pop_contentview, null);
// 生成PopWindow
mPopWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
contentView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (mPopWindow != null && mPopWindow.isShowing()) {
mPopWindow.dismiss();
mPopWindow = null;
}
return false; // 返回值爲true,表示消耗了本次事件,事件不會往後傳遞,false則反之
}
});
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xspacing.popwindow.MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="showPopWindow"
android:text="顯示PopWindow" />
</RelativeLayout>
pop_contentview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="分享微信" />
<Button
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="分享QQ" />
<Button
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="分享新浪微博" />
<Button
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:text="分享朋友圈" />
</LinearLayout>