主界面布局activity_main.xml 就是有左边右边2个imageview生成2个popupwindow
<LinearLayout 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.example.simplepopu.MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#333333">
<ImageButton
android:id="@+id/image_edit_left"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_centerVertical="true"
android:background="@null"
android:src="@drawable/modify" />
<ImageButton
android:id="@+id/image_edit_right"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@null"
android:src="@drawable/modify" />
</RelativeLayout>
</LinearLayout>
先看左边的popupwindow把
contact_edit_popu_layout_left.xml
<?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:orientation="vertical" >
<TextView
android:id="@+id/popuItem1"
style="@style/PopuTextStyleLeft"
android:background="@drawable/apply_centfication_sele_left_top"
android:text="11111" />
<TextView
android:id="@+id/popuItem2"
style="@style/PopuTextStyleLeft"
android:background="@drawable/apply_centfication_sele_left_mid"
android:text="22222" />
<TextView
android:id="@+id/popuItem3"
style="@style/PopuTextStyleLeft"
android:background="@drawable/apply_centfication_sele_left_bottom"
android:text="33333" />
</LinearLayout>
<style name="PopuTextStyle">
<item name="android:layout_width">193dp</item>
<item name="android:layout_height">40dp</item>
<item name="android:gravity">center_vertical</item>
<item name="android:paddingLeft">20dp</item>
<item name="android:textSize">16sp</item>
<item name="android:textColor">#333333</item>
</style>
apply_centfication_sele_left_top.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape>
<corners android:topLeftRadius="5dp" android:topRightRadius="5dp"></corners>
<solid android:color="#e6e6e6" />
</shape></item>
<item><shape>
<corners android:topLeftRadius="5dp" android:topRightRadius="5dp"></corners>
<solid android:color="#ffffff" />
</shape></item>
</selector>
apply_centfication_sele_left_mid.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape>
<solid android:color="#e6e6e6" />
</shape></item>
<item><shape>
<solid android:color="#ffffff" />
</shape></item>
</selector>
apply_centfication_sele_left_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape>
<corners android:bottomLeftRadius="5dp" android:bottomRightRadius="5dp"></corners>
<solid android:color="#e6e6e6" />
</shape></item>
<item><shape>
<corners android:bottomLeftRadius="5dp" android:bottomRightRadius="5dp"></corners>
<solid android:color="#ffffff" />
</shape></item>
</selector>
ContactEditPopuLeft
package com.example.simplepopu;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;
/**
* Created by Safly on 2016/7/4.
*/
public class ContactEditPopuLeft extends PopupWindow implements View.OnClickListener, PopupWindow.OnDismissListener {
private static final String TAG = "ContactEditPopu";
private TextView item1,item2,item3;
private ItemClickListener listener;
private Activity context;
public ContactEditPopuLeft(Activity context,String[] itemTexts){
LayoutInflater mInflater = LayoutInflater.from(context);
View contentView = mInflater.inflate(R.layout.contact_edit_popu_layout_left, null);
this.context = context;
item1 = (TextView) contentView.findViewById(R.id.popuItem1);
item2 = (TextView) contentView.findViewById(R.id.popuItem2);
item3 = (TextView) contentView.findViewById(R.id.popuItem3);
item1.setOnClickListener(this);
item2.setOnClickListener(this);
item3.setOnClickListener(this);
if(itemTexts != null){
int length = itemTexts.length;
this.setContentView(contentView);
switch (length){
//只有1个item--新建联系人
case 1:
item1.setText(itemTexts[0]);
item2.setVisibility(View.GONE);
item3.setVisibility(View.GONE);
break;
//只有2个item--新建联系人、删除联系人
case 2:
item1.setText(itemTexts[0]);
item3.setText(itemTexts[1]);
item2.setVisibility(View.GONE);
break;
case 3:
//只有3个item--新建联系人、编辑联系人、删除联系人
item1.setText(itemTexts[0]);
item2.setText(itemTexts[1]);
item3.setText(itemTexts[2]);
break;
}
}
this.setOnDismissListener(this);
this.setFocusable(true);
this.setOutsideTouchable(true);
this.setWidth(-2);
this.setHeight(-2);
this.setBackgroundDrawable(new BitmapDrawable());
}
@Override
public void onClick(View v) {
if(listener != null){
switch (v.getId()){
case R.id.popuItem1:
listener.item1Listener();
break;
case R.id.popuItem2:
listener.item2Listener();
break;
case R.id.popuItem3:
listener.item3Listener();
break;
}
}
this.dismiss();
}
public void showToggle(View parent, int gravity,int xoff,int yoff){
if(isShowing()){
dismiss();
}else{
showAtLocation(parent,gravity,xoff,yoff);
Window window = ((Activity) context).getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.alpha = 0.6f;
window.setAttributes(params);
}
}
public void setItemListener(ItemClickListener listener){
this.listener = listener;
}
@Override
public void onDismiss() {
Window window = ((Activity) context).getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.alpha = 1.0f;
window.setAttributes(params);
}
public interface ItemClickListener{
void item1Listener();
void item2Listener();
void item3Listener();
}
}
####################################接下来看右边popupwindow的布局###########################
接下来看右边popupwindow的布局
contact_edit_popu_layout_right.xml
<?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:orientation="vertical" >
<TextView
android:id="@+id/popuItem1"
style="@style/PopuTextStyle"
android:background="@drawable/popup_bg_top"
android:text="11111" />
<TextView
android:id="@+id/popuItem2"
style="@style/PopuTextStyle"
android:background="@drawable/apply_centfication_sele_mid"
android:text="22222" />
<TextView
android:id="@+id/popuItem3"
style="@style/PopuTextStyle"
android:background="@drawable/apply_centfication_sele_bottom"
android:text="33333" />
<TextView
android:id="@+id/popuItem4"
style="@style/PopuTextStyle"
android:background="@drawable/apply_centfication_sele_all"
android:text="44444"/>
</LinearLayout>
<style name="PopuTextStyle">
<item name="android:layout_width">193dp</item>
<item name="android:layout_height">40dp</item>
<item name="android:gravity">center_vertical</item>
<item name="android:paddingLeft">20dp</item>
<item name="android:textSize">16sp</item>
<item name="android:textColor">#333333</item>
</style>
popup_bg_top.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/popup_top_press"/>
<item android:state_pressed="false" android:drawable="@drawable/popup_top_normal"/>
<item android:drawable="@drawable/popup_top_normal"/>
</selector>
apply_centfication_sele_mid.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/popup_mid_press"/>
<item android:state_pressed="false" android:drawable="@drawable/popup_mid_normal"/>
<item android:drawable="@drawable/popup_mid_normal"/>
</selector>
apply_centfication_sele_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/popup_bottom_press"/>
<item android:state_pressed="false" android:drawable="@drawable/popup_bottom_normal"/>
<item android:drawable="@drawable/popup_bottom_normal"/>
</selector>
apply_centfication_sele_all.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/popup_all_press"/>
<item android:state_pressed="false" android:drawable="@drawable/popup_all_normal"/>
<item android:drawable="@drawable/popup_all_normal"/>
</selector>
ContactEditPopuRight
package com.example.simplepopu;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;
/**
* Created by Safly on 2016/7/4.
*/
public class ContactEditPopuRight extends PopupWindow implements View.OnClickListener, PopupWindow.OnDismissListener {
private static final String TAG = "ContactEditPopu";
private TextView item1,item2,item3,item4;
private ItemClickListener listener;
public ContactEditPopuRight(Activity context,String[] itemTexts){
LayoutInflater mInflater = LayoutInflater.from(context);
View contentView = mInflater.inflate(R.layout.contact_edit_popu_layout_right, null);
item1 = (TextView) contentView.findViewById(R.id.popuItem1);
item2 = (TextView) contentView.findViewById(R.id.popuItem2);
item3 = (TextView) contentView.findViewById(R.id.popuItem3);
item4 = (TextView) contentView.findViewById(R.id.popuItem4);
item1.setOnClickListener(this);
item2.setOnClickListener(this);
item3.setOnClickListener(this);
item4.setOnClickListener(this);
if(itemTexts != null){
int length = itemTexts.length;
this.setContentView(contentView);
switch (length){
//只有1个item--新建联系人
case 1:
item4.setText(itemTexts[0]);
item1.setVisibility(View.GONE);
item2.setVisibility(View.GONE);
item3.setVisibility(View.GONE);
break;
//只有2个item--新建联系人、删除联系人
case 2:
item1.setText(itemTexts[0]);
item3.setText(itemTexts[1]);
item2.setVisibility(View.GONE);
item4.setVisibility(View.GONE);
break;
case 3:
//只有3个item--新建联系人、编辑联系人、删除联系人
item1.setText(itemTexts[0]);
item2.setText(itemTexts[1]);
item3.setText(itemTexts[2]);
item4.setVisibility(View.GONE);
break;
}
}
this.setOnDismissListener(this);
this.setFocusable(true);
this.setOutsideTouchable(true);
this.setWidth(-2);
this.setHeight(-2);
this.setBackgroundDrawable(new BitmapDrawable());
}
@Override
public void onClick(View v) {
if(listener != null){
switch (v.getId()){
case R.id.popuItem1:
listener.rightItem1Listener();
break;
case R.id.popuItem2:
listener.rightItem2Listener();
break;
case R.id.popuItem3:
listener.rightItem3Listener();
break;
case R.id.popuItem4:
listener.rightItem4Listener();
break;
}
}
this.dismiss();
}
public void showToggle(View parent, int gravity,int xoff,int yoff){
if(isShowing()){
dismiss();
}else{
showAtLocation(parent,gravity,xoff,yoff);
}
}
public void setItemListener(ItemClickListener listener){
this.listener = listener;
}
@Override
public void onDismiss() {
}
public interface ItemClickListener{
void rightItem1Listener();
void rightItem2Listener();
void rightItem3Listener();
void rightItem4Listener();
}
}
最后看主界面代码
MainActivity
package com.example.simplepopu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Toast;
public class MainActivity extends Activity implements
ContactEditPopuLeft.ItemClickListener, ContactEditPopuRight.ItemClickListener{
/**
* 获取状态栏的高度
* @return
*/
public int getStatusHeight() {
int statusHeight = -1;
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int height = Integer.parseInt(clazz.getField("status_bar_height")
.get(object).toString());
statusHeight = getResources().getDimensionPixelSize(height);
} catch (Exception e) {
e.printStackTrace();
}
return statusHeight;
}
private int popupStyle = 1;
private int popupLeftStyle = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
/**
* 右边的popupwindow
*/
findViewById(R.id.image_edit_left).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
ContactEditPopuLeft popu = null;
switch (popupLeftStyle) {
case 2:
popu = new ContactEditPopuLeft(
MainActivity.this, new String[] {
"新建联系人" , "删除联系人"});
popupLeftStyle = 3;
break;
case 3:
popu = new ContactEditPopuLeft(
MainActivity.this, new String[] {
"新建联系人", "编辑联系人" ,"删除联系人"});
popupLeftStyle = 2;
break;
}
// 相对于整个屏幕--x不动(为正向左偏移),y坐标移动标题栏+控件高
popu.showToggle(v, Gravity.TOP | Gravity.LEFT, 0,
(int) (v.getHeight() + getStatusHeight()));
popu.setItemListener(MainActivity.this);
}
});
/**
* 左边的popupwindow
*/
findViewById(R.id.image_edit_right).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContactEditPopuRight popuRight = null;
switch (popupStyle) {
case 1:
popuRight = new ContactEditPopuRight(
MainActivity.this, new String[] {"新建联系人" });
popupStyle = 2;
break;
case 2:
popuRight = new ContactEditPopuRight(
MainActivity.this, new String[] {
"新建联系人" , "删除联系人"});
popupStyle = 3;
break;
case 3:
popuRight = new ContactEditPopuRight(
MainActivity.this, new String[] {
"新建联系人", "编辑联系人" ,"删除联系人"});
popupStyle = 1;
break;
}
// 相对于整个屏幕--x不动(为正向左偏移),y坐标移动标题栏+控件高
popuRight.showToggle(v, Gravity.RIGHT | Gravity.TOP, 0,
(int) (v.getHeight() + getStatusHeight()));
popuRight.setItemListener(MainActivity.this);
}
});
}
@Override
public void item1Listener() {
}
@Override
public void item2Listener() {
}
@Override
public void item3Listener() {
}
/**
* 右边的popupwindow的监听事件
*/
@Override
public void rightItem1Listener() {
// TODO Auto-generated method stub
}
@Override
public void rightItem2Listener() {
// TODO Auto-generated method stub
}
@Override
public void rightItem3Listener() {
// TODO Auto-generated method stub
}
@Override
public void rightItem4Listener() {
// TODO Auto-generated method stub
}
}