直接上图:
1 import android.app.Activity; 2 import android.os.Bundle; 3 import android.view.Gravity; 4 import android.view.MotionEvent; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.widget.LinearLayout; 8 import android.widget.LinearLayout.LayoutParams; 9 import android.widget.PopupWindow; 10 import android.widget.TextView; 11 import android.widget.Toast; 12 13 public class Popupwindow_Activity extends Activity { 14 private PopupWindow popWin; 15 private View view; 16 private LinearLayout linear; 17 private TextView t1,t2,t3; 18 private float x,y; 19 @Override 20 public void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.main); 23 view=getLayoutInflater().inflate(R.layout.popwin, null); 24 t1=(TextView)view.findViewById(R.id.link_text); 25 t2=(TextView)view.findViewById(R.id.link_text2); 26 t3=(TextView)view.findViewById(R.id.link_text3); 27 linear=(LinearLayout)findViewById(R.id.linear); 28 //这里监听事件之所以采用这种方式是因为setContentVIEW(main)所以... 29 t1.setOnClickListener(new OnClickListener() { 30 31 @Override 32 public void onClick(View v) { 33 // TODO Auto-generated method stub 34 Toast.makeText(Popupwindow_Activity.this, "在线", Toast.LENGTH_LONG).show(); 35 } 36 }); 37 t2.setOnClickListener(new OnClickListener() { 38 39 @Override 40 public void onClick(View v) { 41 // TODO Auto-generated method stub 42 Toast.makeText(Popupwindow_Activity.this, "离线", Toast.LENGTH_LONG).show(); 43 } 44 }); 45 t3.setOnClickListener(new OnClickListener() { 46 47 @Override 48 public void onClick(View v) { 49 // TODO Auto-generated method stub 50 Toast.makeText(Popupwindow_Activity.this, "隐身", Toast.LENGTH_LONG).show(); 51 } 52 }); 53 } 54 //初始化popwindow 55 public void initPopWindow(){ 56 if(null==popWin){//(popwin自定义布局文件,popwin宽度,popwin高度)(注:若想指定位置则后两个参数必须给定值不能为WRAP_CONTENT) 57 popWin=new PopupWindow(view, 120, LayoutParams.WRAP_CONTENT); 58 } 59 if(popWin.isShowing()){//如果当前正在显示,则将被处理 60 popWin.dismiss(); 61 } 62 } 63 //以自己作为Anchor,Drawdown风格,显示在正下方 64 public void btn1Click(View v) { 65 initPopWindow(); 66 // popWin.showAsDropDown(v); 67 popWin.showAsDropDown(v,Math.abs(v.getWidth()-popWin.getWidth())/2, 0); 68 } 69 //以自己为Anchor,偏移100,-50 70 public void btn2Click(View v) { 71 initPopWindow(); 72 popWin.showAsDropDown(v,100,-50); 73 } 74 //以屏幕正中为参照,不偏移 75 public void btn3Click(View v) { 76 initPopWindow(); 77 popWin.showAtLocation(linear,Gravity.CENTER, 0, 0); 78 } 79 //以屏幕左上为参照,偏移50,50(包括状态栏) 80 public void btn4Click(View v) { 81 initPopWindow(); 82 //(参照物,垂直水平对齐方式,x偏移,y偏移) 83 popWin.showAtLocation(linear,Gravity.TOP | Gravity.LEFT, 50, 50); 84 } 85 //距屏幕下方一个popwin高度 86 public void btn5Click(View v) { 87 initPopWindow(); 88 popWin.showAtLocation(linear,Gravity.BOTTOM | Gravity.CENTER, 0, view.getHeight()); 89 90 } 91 //这里采用Gestuary手势识别最佳 92 @Override 93 public boolean onTouchEvent(MotionEvent event) { 94 // TODO Auto-generated method stub 95 if(event.getX()==x&&event.getY()==y){ 96 return true; 97 }else{ 98 x=event.getX(); 99 y=event.getY(); 100 } 101 initPopWindow(); 102 popWin.showAtLocation(linear,Gravity.TOP|Gravity.LEFT, (int)event.getX()-popWin.getWidth()/2,(int)event.getY()); 103 System.out.println("view"+view.getHeight()+" h/w "+view.getWidth()); 104 return true; 105 } 106 107 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 android:id="@+id/linear" 7 > 8 <TextView 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:text="@string/hello" 12 /> 13 <Button 14 android:id="@+id/btn1" 15 android:text="以自己作为Anchor,Drawdown风格,显示在正下方" 16 android:onClick="btn1Click" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" /> 19 <Button 20 android:id="@+id/btn2" 21 android:text="以自己为Anchor,偏移100,-50" 22 android:onClick="btn2Click" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" /> 25 <Button 26 android:id="@+id/btn3" 27 android:text="以屏幕正中为参照,不偏移" 28 android:onClick="btn3Click" 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" /> 31 <Button 32 android:id="@+id/btn4" 33 android:text="以屏幕左上为参照,偏移50,50(包括状态栏)" 34 android:onClick="btn4Click" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" /> 37 <Button 38 android:id="@+id/btn5" 39 android:text="距屏幕下方一个popwin高度" 40 android:onClick="btn5Click" 41 android:layout_width="wrap_content" 42 android:layout_height="wrap_content" /> 43 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:orientation="vertical" 5 android:layout_width="wrap_content" 6 android:layout_height="wrap_content" 7 android:background="@drawable/pop_up" 8 android:gravity="center"> 9 <TableLayout 10 android:layout_height="wrap_content" 11 android:layout_width="wrap_content" 12 android:paddingTop="40dp" 13 android:paddingLeft="10dp" 14 android:paddingRight="10dp" 15 android:paddingBottom="10dp" 16 > 17 <TableRow 18 android:layout_height="wrap_content" 19 android:layout_width="wrap_content" 20 android:gravity="left|center_vertical" 21 > 22 <ImageView 23 android:id="@+id/link_img" 24 android:layout_height="40dp" 25 android:layout_width="40dp" 26 android:src="@drawable/qq" 27 android:scaleType="fitCenter" 28 29 /> 30 <TextView 31 android:id="@+id/link_text" 32 android:layout_height="wrap_content" 33 android:layout_width="wrap_content" 34 android:textSize="14sp" 35 android:text="qq在线" 36 /> 37 </TableRow> 38 39 40 41 <TableRow 42 android:layout_height="wrap_content" 43 android:layout_width="fill_parent" 44 android:gravity="left|center_vertical" 45 > 46 <ImageView 47 android:id="@+id/link_img2" 48 android:layout_height="40dp" 49 android:layout_width="40dp" 50 android:src="@drawable/qq2" 51 android:scaleType="fitCenter" 52 /> 53 <TextView 54 android:id="@+id/link_text2" 55 android:layout_height="wrap_content" 56 android:layout_width="wrap_content" 57 android:textSize="14sp" 58 android:text="qq离线" 59 /> 60 </TableRow> 61 62 63 <TableRow 64 android:layout_height="wrap_content" 65 android:layout_width="fill_parent" 66 android:gravity="left|center_vertical" 67 > 68 <ImageView 69 android:id="@+id/link_img3" 70 android:layout_height="40dp" 71 android:layout_width="40dp" 72 android:src="@drawable/qq3" 73 android:scaleType="fitCenter" 74 /> 75 <TextView 76 android:id="@+id/link_text3" 77 android:layout_height="wrap_content" 78 android:layout_width="wrap_content" 79 android:textSize="14sp" 80 android:text="qq隐身" 81 /> 82 </TableRow> 83 84 </TableLayout> 85 86 </LinearLayout>
直接将上面的代码复制到工程中,即可运行。功能比较简单,代码容易理解。如有转载,请注明出处...