├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── dictionaries │ └── gavin.xml ├── gradle.xml ├── kotlinc.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── gavin │ │ └── com │ │ └── popuptest │ │ ├── MainActivity.java │ │ └── TestPopupWindow.java │ └── res │ ├── layout │ ├── activity_main.xml │ └── popup_test.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── smartpopupwindow ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src └── main ├── AndroidManifest.xml ├── java └── com │ └── gavin │ └── com │ └── smartpopupwindow │ ├── HorizontalPosition.java │ ├── SmartPopupWindow.java │ └── VerticalPosition.java └── res └── values └── strings.xml /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/dictionaries/gavin.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/kotlinc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 38 | 39 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 87 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | Android > Lint > Correctness 107 | 108 | 109 | Android > Lint > Performance 110 | 111 | 112 | 113 | 114 | Android 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 136 | 137 | 138 | 139 | 140 | 1.8 141 | 142 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 158 | 159 | 160 | 161 | 162 | 163 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SmartPopupWindow 2 | 用法: 3 | ```java 4 | SmartPopupWindow popupWindow= SmartPopupWindow.Builder 5 | .build(Activity.this, view) 6 | .setAlpha(0.4f) //背景灰度 默认全透明 7 | .setOutsideTouchDismiss(false) //点击外部消失 默认true(消失) 8 | .createPopupWindow(); 9 | popupWindow.showAtAnchorView(view, VerticalPosition.ABOVE, HorizontalPosition.CENTER); 10 | ``` 11 | 水平方向参数HorizontalPosition:LEFT 、 RIGHT 、 ALIGN_LEFT 、 ALIGN_RIGHT、 CENTER 12 | 竖直方向参数VerticalPosition :ABOVE 、 BELOW、 ALIGN_TOP 、 ALIGN_BOTTOM、 CENTER 13 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "com.gavin.com.popuptest" 8 | minSdkVersion 16 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(include: ['*.jar'], dir: 'libs') 24 | compile 'com.android.support:appcompat-v7:25.3.1' 25 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 26 | compile project(':smartpopupwindow') 27 | } 28 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/gavin/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/gavin/com/popuptest/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.gavin.com.popuptest; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.IdRes; 5 | import android.support.v4.widget.PopupWindowCompat; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.view.Gravity; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | import android.widget.RadioGroup; 11 | 12 | import com.gavin.com.smartpopupwindow.HorizontalPosition; 13 | import com.gavin.com.smartpopupwindow.SmartPopupWindow; 14 | import com.gavin.com.smartpopupwindow.VerticalPosition; 15 | 16 | public class MainActivity extends AppCompatActivity { 17 | 18 | private TestPopupWindow mWindow; 19 | 20 | private View mButton; 21 | private View mPopupContentView; 22 | 23 | private int mGravity = Gravity.START; 24 | private int mOffsetX = 0; 25 | private int mOffsetY = 0; 26 | 27 | private boolean useSmartPopup = true; 28 | 29 | @Override 30 | protected void onCreate(Bundle savedInstanceState) { 31 | super.onCreate(savedInstanceState); 32 | setContentView(R.layout.activity_main); 33 | mWindow = new TestPopupWindow(this); 34 | mButton = findViewById(R.id.btn); 35 | mPopupContentView = getLayoutInflater().inflate(R.layout.popup_test, null); 36 | RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rg); 37 | 38 | 39 | mWindow = new TestPopupWindow(this); 40 | View contentView = mWindow.getContentView(); 41 | //需要先测量,PopupWindow还未弹出时,宽高为0 42 | contentView.measure(makeDropDownMeasureSpec(mWindow.getWidth()), makeDropDownMeasureSpec(mWindow.getHeight())); 43 | radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 44 | 45 | @Override 46 | public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { 47 | switch (checkedId) { 48 | case R.id.below_left: 49 | mGravity = Gravity.START; 50 | mOffsetX = -mWindow.getContentView().getMeasuredWidth(); 51 | mOffsetY = 0; 52 | //使用SmartPopup 53 | if (useSmartPopup) 54 | SmartPopupWindow.Builder 55 | .build(MainActivity.this, mPopupContentView) 56 | .createPopupWindow() 57 | .showAtAnchorView(mButton, VerticalPosition.BELOW, HorizontalPosition.LEFT); 58 | break; 59 | case R.id.below_align_left: 60 | mGravity = Gravity.START; 61 | mOffsetX = 0; 62 | mOffsetY = 0; 63 | //使用SmartPopup 64 | if (useSmartPopup) 65 | SmartPopupWindow.Builder 66 | .build(MainActivity.this, mPopupContentView) 67 | .createPopupWindow() 68 | .showAtAnchorView(mButton, VerticalPosition.BELOW, HorizontalPosition.ALIGN_LEFT); 69 | 70 | break; 71 | case R.id.below_right: 72 | mGravity = Gravity.END; 73 | mOffsetX = 0; 74 | mOffsetY = 0; 75 | //使用SmartPopup 76 | if (useSmartPopup) 77 | SmartPopupWindow.Builder 78 | .build(MainActivity.this, mPopupContentView) 79 | .createPopupWindow() 80 | .showAtAnchorView(mButton, VerticalPosition.BELOW, HorizontalPosition.RIGHT); 81 | 82 | break; 83 | case R.id.below_align_right: 84 | mGravity = Gravity.START; 85 | mOffsetX = mButton.getWidth() - mWindow.getContentView().getMeasuredWidth(); 86 | mOffsetY = 0; 87 | //使用SmartPopup 88 | if (useSmartPopup) 89 | SmartPopupWindow.Builder 90 | .build(MainActivity.this, mPopupContentView) 91 | .createPopupWindow() 92 | .showAtAnchorView(mButton, VerticalPosition.BELOW, HorizontalPosition.ALIGN_RIGHT); 93 | 94 | break; 95 | case R.id.below_center: 96 | mGravity = Gravity.START; 97 | mOffsetX = Math.abs(mWindow.getContentView().getMeasuredWidth() - mButton.getWidth()) / 2; 98 | mOffsetY = 0; 99 | //使用SmartPopup 100 | if (useSmartPopup) 101 | SmartPopupWindow.Builder 102 | .build(MainActivity.this, mPopupContentView) 103 | .createPopupWindow() 104 | .showAtAnchorView(mButton, VerticalPosition.BELOW, HorizontalPosition.CENTER); 105 | 106 | break; 107 | case R.id.above_center: 108 | mGravity = Gravity.START; 109 | mOffsetX = Math.abs(mWindow.getContentView().getMeasuredWidth() - mButton.getWidth()) / 2; 110 | mOffsetY = -(mWindow.getContentView().getMeasuredHeight() + mButton.getHeight()); 111 | //使用SmartPopup 112 | if (useSmartPopup) 113 | SmartPopupWindow.Builder 114 | .build(MainActivity.this, mPopupContentView) 115 | .createPopupWindow() 116 | .showAtAnchorView(mButton, VerticalPosition.ABOVE, HorizontalPosition.CENTER); 117 | 118 | break; 119 | case R.id.left_center: 120 | mGravity = Gravity.START; 121 | mOffsetX = -mWindow.getContentView().getMeasuredWidth(); 122 | mOffsetY = -(mWindow.getContentView().getMeasuredHeight() + mButton.getHeight()) / 2; 123 | //使用SmartPopup 124 | if (useSmartPopup) 125 | SmartPopupWindow.Builder 126 | .build(MainActivity.this, mPopupContentView) 127 | .createPopupWindow() 128 | .showAtAnchorView(mButton, VerticalPosition.CENTER, HorizontalPosition.LEFT); 129 | 130 | break; 131 | case R.id.right_center: 132 | mGravity = Gravity.END; 133 | mOffsetX = 0; 134 | mOffsetY = -(mWindow.getContentView().getMeasuredHeight() + mButton.getHeight()) / 2; 135 | //使用SmartPopup 136 | if (useSmartPopup) 137 | SmartPopupWindow.Builder 138 | .build(MainActivity.this, mPopupContentView) 139 | .createPopupWindow() 140 | .showAtAnchorView(mButton, VerticalPosition.CENTER, HorizontalPosition.RIGHT); 141 | 142 | break; 143 | } 144 | } 145 | }); 146 | mButton.setOnClickListener(new View.OnClickListener() { 147 | @Override 148 | public void onClick(View v) { 149 | showPopup(); 150 | } 151 | }); 152 | } 153 | 154 | @SuppressWarnings("ResourceType") 155 | private static int makeDropDownMeasureSpec(int measureSpec) { 156 | int mode; 157 | if (measureSpec == ViewGroup.LayoutParams.WRAP_CONTENT) { 158 | mode = View.MeasureSpec.UNSPECIFIED; 159 | } else { 160 | mode = View.MeasureSpec.EXACTLY; 161 | } 162 | return View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.getSize(measureSpec), mode); 163 | } 164 | 165 | private void showPopup() { 166 | PopupWindowCompat.showAsDropDown(mWindow, mButton, mOffsetX, mOffsetY, mGravity); 167 | } 168 | 169 | } 170 | -------------------------------------------------------------------------------- /app/src/main/java/com/gavin/com/popuptest/TestPopupWindow.java: -------------------------------------------------------------------------------- 1 | package com.gavin.com.popuptest; 2 | 3 | import android.content.Context; 4 | import android.graphics.Color; 5 | import android.graphics.drawable.ColorDrawable; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.PopupWindow; 10 | 11 | /** 12 | * Created by gavin 13 | * Created date 17/9/13 14 | */ 15 | 16 | public class TestPopupWindow extends PopupWindow { 17 | 18 | public TestPopupWindow(Context context) { 19 | super(context); 20 | setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); 21 | setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); 22 | setOutsideTouchable(true); 23 | setFocusable(true); 24 | setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 25 | View contentView = LayoutInflater.from(context).inflate(R.layout.popup_test, null, false); 26 | setContentView(contentView); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 21 | 22 | 28 | 29 | 34 | 35 | 40 | 41 | 46 | 47 | 52 | 53 | 58 | 59 | 64 | 65 | 66 | 67 |