Android popupwindow 示例程序一

转自http://blog.csdn.net/qq_16064871/article/details/46461459

经过多番测试实践,实现了popupwindow 弹出在指定控件的下方。代码上有注释,有需要注意的地方。popupwindow 有自已的布局,里面控件的监听实现都有。接下来看代码实现。

项目资源下载:点击这里

TestPopwindow2.class

[java] view plaincopy
  1. package com.example.popwindowdemo;  
  2.   
  3. import android.app.ActionBar.LayoutParams;  
  4. import android.app.Activity;  
  5. import android.content.Context;  
  6. import android.graphics.drawable.BitmapDrawable;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.widget.PopupWindow;  
  10.   
  11. public class TestPopwindow2 extends PopupWindow {  
  12.     // 根视图  
  13.     private View mRootView;  
  14.     // LayoutInflater  
  15.     LayoutInflater mInflater;  
  16.   
  17.     public TestPopwindow2(Activity context) {  
  18.         super(context);  
  19.         mInflater = (LayoutInflater) context  
  20.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  21.         mRootView = mInflater.inflate(R.layout.test_popwindow_2, null);  
  22.         setContentView(mRootView);  
  23.   
  24.         this.setWidth(LayoutParams.WRAP_CONTENT);  
  25.         this.setHeight(LayoutParams.WRAP_CONTENT);  
  26.   
  27.         // 设置PopUpWindow弹出的相关属性  
  28.         setTouchable(true);  
  29.         setOutsideTouchable(true);  
  30.         setFocusable(true);  
  31.         setBackgroundDrawable(new BitmapDrawable(context.getResources()));  
  32.         update();  
  33.   
  34.         getContentView().setFocusableInTouchMode(true);  
  35.         getContentView().setFocusable(true);  
  36.         setAnimationStyle(R.style.AppBaseTheme);  
  37.     }  
  38. }  
MainActivity.class

[java] view plaincopy
  1. package com.example.popwindowdemo;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Gravity;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.PopupWindow.OnDismissListener;  
  10. import android.widget.Toast;  
  11.   
  12. public class MainActivity extends Activity implements OnClickListener,  
  13.         OnDismissListener {  
  14.   
  15.     private TestPopwindow2 mTestPopwindow2 = null;  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.   
  22.         InitUI();  
  23.   
  24.     }  
  25.   
  26.     private void InitUI() {  
  27.         // 实例化TestPopwindow2  
  28.         mTestPopwindow2 = new TestPopwindow2(this);  
  29.         // 设置点击其他位置mTestPopwindow2消失  
  30.         mTestPopwindow2.setOnDismissListener(this);  
  31.   
  32.         Button buttonTest2 = (Button) findViewById(R.id.buttonTest2);  
  33.         buttonTest2.setOnClickListener(this);  
  34.     }  
  35.   
  36.     private void OnPopwindowTest2() {  
  37.         if (mTestPopwindow2 == null)  
  38.             return;  
  39.   
  40.         // location获得控件的位置  
  41.         int[] location = new int[2];  
  42.         View v = findViewById(R.id.buttonTest2);  
  43.         if (v != null)  
  44.             v.getLocationOnScreen(location);   // 控件在屏幕的位置  
  45.         mTestPopwindow2.setAnimationStyle(R.style.AppBaseTheme);  
  46.           
  47.         // mTestPopwindow2弹出在某控件(button)的下面  
  48.         mTestPopwindow2.showAtLocation(v, Gravity.TOP | Gravity.LEFT,  
  49.                 location[0] - v.getWidth(), location[1] + v.getHeight());  
  50.     }  
  51.   
  52.     // mTestPopwindow2布局控件的监听  
  53.     public void OnclickTestListener(View view) {  
  54.         switch (view.getId()) {  
  55.         case R.id.layoutSeclect1:  
  56.             Toast.makeText(this"系统热门方案", Toast.LENGTH_SHORT).show();  
  57.             break;  
  58.         case R.id.layoutSeclect2:  
  59.             Toast.makeText(this"个人热门方案", Toast.LENGTH_SHORT).show();  
  60.             break;  
  61.         default:  
  62.             break;  
  63.         }  
  64.         if (mTestPopwindow2 != null)  
  65.             mTestPopwindow2.dismiss();  
  66.     }  
  67.   
  68.     @Override  
  69.     public void onClick(View v) {  
  70.         switch (v.getId()) {  
  71.         case R.id.buttonTest2:  
  72.             OnPopwindowTest2();  
  73.             break;  
  74.         default:  
  75.             break;  
  76.         }  
  77.     }  
  78.   
  79.     // 点击其他地方消失  
  80.     @Override  
  81.     public void onDismiss() {  
  82.     }  
  83. }  
test_popwindow_2.xml
[html] view plaincopy
  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="#0000" >  
  6.   
  7.     <!--  
  8.   popupwimdow的布局注意不能使用类似          
  9.     android:layout_marginTop=""这样属性,特别是相对布局时候.   
  10.   布局或者控件整体靠左等都会影响popupwimdow定位使用  
  11.     -->  
  12.   
  13.     <LinearLayout  
  14.         android:id="@+id/hot_layout"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:background="@drawable/title_function_bg"  
  18.         android:orientation="vertical" >  
  19.   
  20.         <LinearLayout  
  21.             android:id="@+id/layoutSeclect1"  
  22.             android:layout_width="match_parent"  
  23.             android:layout_height="wrap_content"  
  24.             android:clickable="true"  
  25.             android:onClick="OnclickTestListener" >  
  26.   
  27.             <TextView  
  28.                 android:layout_width="wrap_content"  
  29.                 android:layout_height="wrap_content"  
  30.                 android:padding="8dp"  
  31.                 android:text="系统热门方案"  
  32.                 android:textColor="#fff"  
  33.                 android:textSize="18sp" />  
  34.         </LinearLayout>  
  35.   
  36.         <LinearLayout  
  37.             android:id="@+id/layoutSeclect2"  
  38.             android:layout_width="match_parent"  
  39.             android:layout_height="wrap_content"  
  40.             android:clickable="true"  
  41.             android:onClick="OnclickTestListener" >  
  42.   
  43.             <TextView  
  44.                 android:layout_width="wrap_content"  
  45.                 android:layout_height="wrap_content"  
  46.                 android:padding="8dp"  
  47.                 android:text="个人热门方案"  
  48.                 android:textColor="#fff"  
  49.                 android:textSize="18sp" />  
  50.         </LinearLayout>  
  51.     </LinearLayout>  
  52.   
  53. </LinearLayout>  
activity_main.xml

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/LinearLayoutMain"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/hello_world" />  
  12.   
  13.   
  14.     <LinearLayout  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:gravity="right">  
  18.           
  19.     <Button  
  20.         android:id="@+id/buttonTest2"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="测试2" />  
  24.     </LinearLayout>  
  25.   
  26. </LinearLayout>  
效果图如下

项目资源下载:点击这里

转载请注明出处的博客网址: http://blog.csdn.net/qq_16064871
如有转载未注明出处博客网址,或者没有得到作者的同意。作者持有版权所有权。



原文地址:https://www.cnblogs.com/walccott/p/4957593.html