Android的對話框有兩種:PopupWindow和AlertDialog。它們的不一樣點在於: java
PopupWindow的位置按照有無偏移分,能夠分爲偏移和無偏移兩種;按照參照物的不一樣,能夠分爲相對於某個控件(Anchor錨)和相對於父控件。具體以下 android
下面經過一個Demo講解(解釋看註釋): app
main.xml ide
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="以本身爲Anchor,不偏移" />
<Button
android:id="@+id/button02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="以本身爲Anchor,有偏移" />
<Button
android:id="@+id/button03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="以屏幕中心爲參照,不偏移(正中間)" />
<Button
android:id="@+id/button04"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="以屏幕下方爲參照,下方中間" />
</LinearLayout> this
popup_window.xml spa
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00FF00"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="選擇狀態:"
android:textColor="@android :color/white"
android:textSize="20px" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton android:text="在線" />
<RadioButton android:text="離線" />
<RadioButton android:text="隱身" />
</RadioGroup>
</LinearLayout> .net
PopupWindowDemoActivity.java 線程
package com.tianjf;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class PopupWindowDemoActivity extends Activity implements OnClickListener,
OnCheckedChangeListener {
private Button mbutton01;
private Button mbutton02;
private Button mbutton03;
private Button mbutton04;
private PopupWindow mPopupWindow;
// 屏幕的width
private int mScreenWidth;
// 屏幕的height
private int mScreenHeight;
// PopupWindow的width
private int mPopupWindowWidth;
// PopupWindow的height
private int mPopupWindowHeight;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mbutton01 = (Button) findViewById(R.id.button01);
mbutton02 = (Button) findViewById(R.id.button02);
mbutton03 = (Button) findViewById(R.id.button03);
mbutton04 = (Button) findViewById(R.id.button04);
mbutton01.setOnClickListener(this);
mbutton02.setOnClickListener(this);
mbutton03.setOnClickListener(this);
mbutton04.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
// 相對某個控件的位置(正左下方),無偏移
case R.id.button01:
getPopupWindowInstance();
mPopupWindow.showAsDropDown(v);
break;
// 相對某個控件的位置(正左下方),有偏移
case R.id.button02:
getPopupWindowInstance();
mPopupWindow.showAsDropDown(v, 50, 50);// X、Y方向各偏移50
break;
// 相對於父控件的位置,無偏移
case R.id.button03:
getPopupWindowInstance();
mPopupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
break;
// 相對於父控件的位置,有偏移
case R.id.button04:
getPopupWindowInstance();
mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 50);
break;
default:
break;
}
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
mPopupWindow.dismiss();
}
/*
* 獲取PopupWindow實例
*/
private void getPopupWindowInstance() {
if (null != mPopupWindow) {
mPopupWindow.dismiss();
return;
} else {
initPopuptWindow();
}
}
/*
* 建立PopupWindow
*/
private void initPopuptWindow() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View popupWindow = layoutInflater.inflate(R.layout.popup_window, null);
RadioGroup radioGroup = (RadioGroup) popupWindow.findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(this);
// 建立一個PopupWindow
// 參數1:contentView 指定PopupWindow的內容
// 參數2:width 指定PopupWindow的width
// 參數3:height 指定PopupWindow的height
mPopupWindow = new PopupWindow(popupWindow, 100, 130);
// 獲取屏幕和PopupWindow的width和height
mScreenWidth = getWindowManager().getDefaultDisplay().getWidth();
mScreenWidth = getWindowManager().getDefaultDisplay().getHeight();
mPopupWindowWidth = mPopupWindow.getWidth();
mPopupWindowHeight = mPopupWindow.getHeight();
}
} orm