实现将一个View显示在某一位置,而且是浮于当前窗口
首先要有一个要显示的view的布局,可以是任意View,包括ViewGroup
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:background="#d3d3d3" 6 android:gravity="center_horizontal" 7 android:orientation="vertical" > 8 9 10 <TextView 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:gravity="center" 14 android:id="@+id/textView" 15 android:text="文本内容" 16 /> 17 </LinearLayout>
然后主界面布局文件
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context=".MainActivity" > 6 7 <LinearLayout 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:orientation="horizontal" 11 > 12 <!-- 区域 --> 13 <Button 14 android:layout_width="0dp" 15 android:layout_weight="1" 16 android:gravity="center" 17 android:background="#0000" 18 android:layout_height="wrap_content" 19 android:text="区域" 20 android:drawableRight="@drawable/title_bar_arrow_nor" 21 android:id="@+id/btn_1" 22 /> 23 <!-- 类别 --> 24 <Button 25 android:background="#0000" 26 android:layout_width="0dp" 27 android:layout_weight="1" 28 android:layout_height="wrap_content" 29 android:text="类别" 30 android:drawableRight="@drawable/title_bar_arrow_nor" 31 android:id="@+id/btn_2" 32 /> 33 34 <!-- 价格 --> 35 <Button 36 android:background="#0000" 37 android:layout_width="0dp" 38 android:layout_weight="1" 39 android:layout_height="wrap_content" 40 android:text="价格" 41 android:drawableRight="@drawable/title_bar_arrow_nor" 42 android:id="@+id/btn_3" 43 /> 44 45 <!-- 更多 --> 46 <Button 47 android:background="#0000" 48 android:layout_width="0dp" 49 android:layout_weight="1" 50 android:layout_height="wrap_content" 51 android:text="更多" 52 android:drawableRight="@drawable/title_bar_arrow_nor" 53 android:id="@+id/btn_4" 54 /> 55 56 </LinearLayout> 57 58 </RelativeLayout>
主activity
1 package com.example.aaa; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.view.ViewGroup; 8 import android.widget.Button; 9 import android.widget.PopupWindow; 10 import android.widget.TextView; 11 12 public class MainActivity extends Activity implements OnClickListener { 13 14 private PopupWindow pop; 15 16 private Button btn1; 17 private Button btn2; 18 private Button btn3; 19 private Button btn4; 20 21 private TextView text; 22 23 View view; 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 29 initView(); 30 initPopWindow(); 31 32 } 33 34 private void initView() { 35 // TODO Auto-generated method stub 36 btn1 = (Button) findViewById(R.id.btn_1); 37 btn2 = (Button) findViewById(R.id.btn_2); 38 btn3 = (Button) findViewById(R.id.btn_3); 39 btn4 = (Button) findViewById(R.id.btn_4); 40 btn1.setOnClickListener(this); 41 btn2.setOnClickListener(this); 42 btn3.setOnClickListener(this); 43 btn4.setOnClickListener(this); 44 } 45 46 47 private void initPopWindow() { 48 // TODO Auto-generated method stub 49 //根据layout创建弹出界面 50 view = this.getLayoutInflater().inflate(R.layout.popup_window, null); 51 pop = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT, 52 ViewGroup.LayoutParams.WRAP_CONTENT); 53 text = (TextView) view.findViewById(R.id.textView); 54 55 pop.setOutsideTouchable(true); 56 view.setOnClickListener(new View.OnClickListener() { 57 @Override 58 public void onClick(View v) { 59 // TODO Auto-generated method stub 60 pop.dismiss(); 61 } 62 }); 63 64 } 65 66 @Override 67 public void onClick(View v) { 68 // TODO Auto-generated method stub 69 switch (v.getId()) { 70 case R.id.btn_1: 71 text.setText("区域"); 72 if(pop.isShowing()) 73 pop.dismiss(); 74 else 75 pop.showAsDropDown(v); 76 break; 77 case R.id.btn_2: 78 text.setText("类别"); 79 if(pop.isShowing()) 80 pop.dismiss(); 81 else 82 pop.showAsDropDown(v); 83 break; 84 case R.id.btn_3: 85 text.setText("价格"); 86 if(pop.isShowing()) 87 pop.dismiss(); 88 else 89 pop.showAsDropDown(v); 90 break; 91 case R.id.btn_4: 92 text.setText("更多"); 93 if(pop.isShowing()) 94 pop.dismiss(); 95 else 96 pop.showAsDropDown(v); 97 break; 98 99 default: 100 break; 101 } 102 } 103 104 }
出现位置的几个方法
showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移 showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移 showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
效果图: