1.使用 popwindow实现如下效果:
2.代码如下:MainActivity.java
[java] view plaincopy
- package mtpop.window.main;
- import android.app.Activity;
- import android.content.Context;
- import android.content.res.Resources;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.PopupWindow;
- import android.widget.PopupWindow.OnDismissListener;
- import android.widget.TextView;
- public class MainActivity extends Activity
- {
- private static final String TAG = "MainActivity";
- private Button button;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button = (Button) findViewById(R.id.button);
- button.setOnClickListener(new OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- // 显示 popupWindow
- PopupWindow popupWindow = makePopupWindow(MainActivity.this);
- int[] xy = new int[2];
- button.getLocationOnScreen(xy);
- popupWindow.showAtLocation(button,Gravity.RIGHT|Gravity.TOP,-xy[0]/2,xy[1]+button.getWidth());
- //popupWindow.showAsDropDown(button,0, 0);
- }
- });
- }
- // 创建一个包含自定义view的PopupWindow
- private PopupWindow makePopupWindow(Context cx)
- {
- PopupWindow window;
- window = new PopupWindow(cx);
- //View contentView = LayoutInflater.from(this).inflate(R.layout.popwindow, null);
- //window.setContentView(contentView);
- Button b1 = new Button(this);
- b1.setText("first");
- b1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
- Button b2 = new Button(this);
- b2.setText("Second");
- b2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
- LinearLayout linearLayout = new LinearLayout(this);
- linearLayout.addView(b1);
- linearLayout.addView(b2);
- linearLayout.setOrientation(LinearLayout.VERTICAL);
- window.setContentView(linearLayout);
- window.setBackgroundDrawable(getResources().getDrawable(R.drawable.pop_bg));
- window.setWidth(DisplayManager.dipToPixel(150));
- window.setHeight(DisplayManager.dipToPixel(150));
- // 设置PopupWindow外部区域是否可触摸
- window.setFocusable(true); //设置PopupWindow可获得焦点
- window.setTouchable(true); //设置PopupWindow可触摸
- window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸
- return window;
- }
- }
-
package mtpop.window.main; -
import android.app.Activity; -
import android.content.Context; -
import android.content.res.Resources; -
import android.os.Bundle; -
import android.view.Gravity; -
import android.view.LayoutInflater; -
import android.view.View; -
import android.view.View.OnClickListener; -
import android.view.ViewGroup.LayoutParams; -
import android.widget.Button; -
import android.widget.LinearLayout; -
import android.widget.PopupWindow; -
import android.widget.PopupWindow.OnDismissListener; -
import android.widget.TextView; -
public class MainActivity extends Activity -
{ -
private static final String TAG = "MainActivity"; -
private Button button; -
@Override -
public void onCreate(Bundle savedInstanceState) -
{ -
super.onCreate(savedInstanceState); -
setContentView(R.layout.main); -
button = (Button) findViewById(R.id.button); -
button.setOnClickListener(new OnClickListener() -
{ -
@Override -
public void onClick(View v) -
{ -
// 显示 popupWindow -
PopupWindow popupWindow = makePopupWindow(MainActivity.this); -
int[] xy = new int[2]; -
button.getLocationOnScreen(xy); -
popupWindow.showAtLocation(button,Gravity.RIGHT|Gravity.TOP,-xy[0]/2,xy[1]+button.getWidth()); -
//popupWindow.showAsDropDown(button,0, 0); -
} -
}); -
} -
// 创建一个包含自定义view的PopupWindow -
private PopupWindow makePopupWindow(Context cx) -
{ -
PopupWindow window; -
window = new PopupWindow(cx); -
//View contentView = LayoutInflater.from(this).inflate(R.layout.popwindow, null); -
//window.setContentView(contentView); -
Button b1 = new Button(this); -
b1.setText("first"); -
b1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); -
Button b2 = new Button(this); -
b2.setText("Second"); -
b2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); -
LinearLayout linearLayout = new LinearLayout(this); -
linearLayout.addView(b1); -
linearLayout.addView(b2); -
linearLayout.setOrientation(LinearLayout.VERTICAL); -
window.setContentView(linearLayout); -
window.setBackgroundDrawable(getResources().getDrawable(R.drawable.pop_bg)); -
window.setWidth(DisplayManager.dipToPixel(150)); -
window.setHeight(DisplayManager.dipToPixel(150)); -
// 设置PopupWindow外部区域是否可触摸 -
window.setFocusable(true); //设置PopupWindow可获得焦点 -
window.setTouchable(true); //设置PopupWindow可触摸 -
window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸 -
return window; -
} -
}
3. main.xml
[html] view plaincopy
- <?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" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="@android:color/darker_gray"
- android:orientation="horizontal" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="Title" />
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="click" />
- </LinearLayout>
- </LinearLayout>
-
<?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" > -
<LinearLayout -
android:layout_width="fill_parent" -
android:layout_height="wrap_content" -
android:background="@android:color/darker_gray" -
android:orientation="horizontal" > -
<TextView -
android:layout_width="fill_parent" -
android:layout_height="wrap_content" -
android:layout_weight="1" -
android:text="Title" /> -
<Button -
android:id="@+id/button" -
android:layout_width="wrap_content" -
android:layout_height="wrap_content" -
android:text="click" /> -
</LinearLayout> -
</LinearLayout>
注意:
* 新建一个popupWindow弹出框 popupWindow是一个阻塞式的弹出框,这就意味着在我们退出这个弹出框之前,程序会一直等待,
* 这和AlertDialog不同哦,AlertDialog是非阻塞式弹出框,AlertDialog弹出的时候,后台可是还可以做其他事情的哦。 *
******************************************************************
1.PopupWindow的隐藏
[java] view plaincopy
- final PopupWindow window = mPageStatWin;
- if(null != window && window.isShowing()) {
- win.dismiss();
- }
-
final PopupWindow window = mPageStatWin; -
if(null != window && window.isShowing()) { -
win.dismiss(); -
}
2.Popupwindow的显示及位置设置
window.showAtLocation(parent, Gravity.RIGHT | Gravity.BOTTOM, 10,10);
第一个参数指定PopupWindow的锚点view,即依附在哪个view上。
第二个参数指定起始点为parent的右下角,第三个参数设置以parent的右下角为原点,向左、上各偏移10像素。
//将PopupWindow作为anchor的下拉窗口显示。即在anchor的左下角显示
window.showAsDropDown(anchor);
//xoff,yoff基于anchor的左下角进行偏移。
window.showAsDropDown(anchor, xoff, yoff);
如果没有充足的空间显示PopupWindow,那么PopupWindow的左下角将位于anchor的左上角来显示。
* popupWindow.showAsDropDown(View view)弹出对话框,位置在紧挨着view组件
* showAsDropDown(View anchor, int xoff, int yoff)弹出对话框,位置在紧挨着view组件,x y 代表着偏移量
* showAtLocation(View parent, int gravity, int x, int y)弹出对话框
* parent 父布局 gravity 依靠父布局的位置如Gravity.CENTER x y 坐标值
[java] view plaincopy
- window.setWidth(DisplayManager.dipToPixel(150));
- window.setHeight(DisplayManager.dipToPixel(150));
- // 设置PopupWindow外部区域是否可触摸
- window.setFocusable(true); //设置PopupWindow可获得焦点
- window.setTouchable(true); //设置PopupWindow可触摸
- window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸
-
window.setWidth(DisplayManager.dipToPixel(150)); -
window.setHeight(DisplayManager.dipToPixel(150)); -
// 设置PopupWindow外部区域是否可触摸 -
window.setFocusable(true); //设置PopupWindow可获得焦点 -
window.setTouchable(true); //设置PopupWindow可触摸 -
window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸
设置其可以获得焦点,可触摸,外部区域可触摸(很重要)