该弹出款效果如下:(箭头的样式是图片做背景的)
需要的朋友可以继续向下看代码:
下面是其Activity代码
public class DRMPTestActivity extends Activity implements OnLongClickListener,OnClickListener{
/** Called when the activity is first created. */
PopupWindow pop ;
Button deleteButton;
Button addButton;
Button editButton;
Button saveButton;
Button upButton;
MyTextView selectview ;//当前正在处理的view
MyTextView text_1;
MyTextView text_2;
MyTextView text_3;
MyTextView text_4;
String fileName = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater inflater = LayoutInflater.from(this);
// 引入窗口配置文件
View view = inflater.inflate(R.layout.popupwindow, null);
text_1 = (MyTextView) findViewById(R.id.text1);
text_1.setOnLongClickListener(this);
text_1.setBackgroundColor(Color.WHITE);
text_2 = (MyTextView) findViewById(R.id.text2);
text_2.setOnLongClickListener(this);
text_2.setBackgroundColor(Color.WHITE);
text_3 = (MyTextView) findViewById(R.id.text3);
text_3.setOnLongClickListener(this);
text_3.setBackgroundColor(Color.WHITE);
text_4 = (MyTextView) findViewById(R.id.text4);
text_4.setOnLongClickListener(this);
text_4.setBackgroundColor(Color.WHITE);
deleteButton = (Button) view.findViewById(R.id.delete);
deleteButton.setOnClickListener(this);
addButton = (Button) view.findViewById(R.id.add);
addButton.setOnClickListener(this);
editButton = (Button) view.findViewById(R.id.edit);
editButton.setOnClickListener(this);
upButton = (Button) findViewById(R.id.upButton);
upButton.setOnClickListener(this);
// 创建PopupWindow对象
// 此处之所以给了 PopupWindow一个固定的宽度 是因为我要让 PopupWindow的中心位置对齐TextView的中心位置
pop = new PopupWindow(view, dipTopx(this, 100),ViewGroup.LayoutParams.WRAP_CONTENT);
// 需要设置一下此参数,点击外边可消失
pop.setBackgroundDrawable(new BitmapDrawable());
pop.setOutsideTouchable(true);
//设置点击窗口外边窗口消失
pop.setOutsideTouchable(true);
// 设置此参数获得焦点,否则无法点击
pop.setFocusable(true);
}
/* (non-Javadoc)
* @see android.view.View.OnLongClickListener#onLongClick(android.view.View)
*/
@Override
public boolean onLongClick(View v) {
selectview = (MyTextView) v;
int color = selectview.getBackgroundColor();
//white代表是未添加
if(color != Color.WHITE){
addButton.setVisibility(View.GONE);
editButton.setVisibility(View.VISIBLE);
deleteButton.setVisibility(View.VISIBLE);
}else{
addButton.setVisibility(View.VISIBLE);
editButton.setVisibility(View.GONE);
deleteButton.setVisibility(View.GONE);
}
if(pop.isShowing()) {
// 隐藏窗口,如果设置了点击窗口外小时即不需要此方式隐藏
pop.dismiss();
} else {
// 显示窗口 设置弹出效果
pop.showAsDropDown(v,v.getWidth()/2-pop.getWidth()/2, 0);
}
return false;
}
//为了计算使popupwindow与目标view中间对其
public static int dipTopx(Context context, float dipValue){
final float scale = context.getResources().getDisplayMetrics().density;
return (int)(dipValue * scale + 0.5f);
}
/* (non-Javadoc)
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.delete:
selectview.setText("");
selectview.setBackgroundColor(Color.WHITE);
pop.dismiss();
break;
case R.id.add:
selectview.setBackgroundColor(Color.BLUE);
pop.dismiss();
break;
case R.id.edit:
selectview.setBackgroundColor(Color.GREEN);
pop.dismiss();
break;
}
}
main.xml代码:
<?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" >
<TableLayout
android:id="@+id/tablelayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#dedcd2"
android:stretchColumns="*" >
<TableRow
android:layout_margin="0.5dip"
android:layout_height="100dip"
android:id="@+id/tableRow"
android:background="#dedcd2" >
<com.cmdi.util.MyTextView
android:layout_margin="1dip"
android:layout_height="fill_parent"
android:id="@+id/text1"
android:text=""
android:textSize="20dip"
android:textStyle="bold" />
<com.cmdi.util.MyTextView
android:layout_margin="1dip"
android:layout_height="fill_parent"
android:text=""
android:id="@+id/text2"
android:textSize="20dip"
android:textStyle="bold" />
<com.cmdi.util.MyTextView
android:layout_margin="1dip"
android:layout_height="fill_parent"
android:text=""
android:id="@+id/text3"
android:textSize="20dip"
android:textStyle="bold" />
<com.cmdi.util.MyTextView
android:layout_margin="1dip"
android:layout_height="100dip"
android:text=""
android:id="@+id/text4"
android:textSize="20dip"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<Button
android:text="保存"
android:id="@+id/save"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<!-- <Button
android:text="编辑"
android:id="@+id/editButton"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/> -->
<Button
android:text="上传"
android:id="@+id/upButton"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
popupWindow.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="horizontal"
android:background="@drawable/ba">
<Button
android:id="@+id/delete"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#00000000"
android:textColor="#FFFFFF"
android:layout_marginTop="5dip"
android:textSize="20dip"
android:gravity="center_horizontal"
android:layout_gravity="center"
android:text="删除" />
<Button
android:id="@+id/add"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#00000000"
android:textColor="#FFFFFF"
android:layout_marginTop="5dip"
android:textSize="20dip"
android:gravity="center_horizontal"
android:layout_gravity="center"
android:text="添加" />
<Button
android:id="@+id/edit"
android:background="#00000000"
android:textColor="#FFFFFF"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:textSize="20dip"
android:gravity="center_horizontal"
android:layout_gravity="center"
android:text="修改" />
</LinearLayout>
以上是其主要代码,想下载DEMO的可以到这个地址
http://download.csdn.net/detail/yusewuhen/6653785下载,注意由于这个Demo还有文件创建、上传功能所以还需要你自己修改,选择其中有用的部分