对于自定义窗口,Android提供了PopupWindow,简单实用。
下面我们来看看:
PopupWindow的构造函数
public PopupWindow(View contentView, int width, int height, boolean focusable)
其中contentView为要显示的view,width和height为宽和高,值为像素值,可以是MATCHT_PARENT和WRAP_CONTENT
来设置,如果focusable为false,在一个Activity弹出一个PopupWindow,按返回键,由于PopupWindow没有焦点,会直接退出Activity。如果focusable为true,PopupWindow弹出后,所有的触屏和物理按键都有PopupWindows处理。
如果PopupWindow中有Editor的话,focusable要为true。

下面来看一个简单的demo

主界面:就简单放一个button,点击弹出我们需要的PopupWindow

[html] view plaincopyprint?

  1. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

  2. xmlns:tools="http://schemas.android.com/tools"

  3. android:id="@+id/container"

  4. android:layout_width="match_parent"

  5. android:layout_height="match_parent" >

  6. <Button

  7. android:id="@+id/btnOpen"

  8. android:layout_width="wrap_content"

  9. android:layout_height="wrap_content"

  10. android:layout_centerInParent="true"

  11. android:text="@string/app_name"/>

  12. </RelativeLayout>


popupwindow 界面:放了2个button 和一个文本框,用来输入值

 

 

[html] view plaincopyprint?

  1. <?xml version="1.0"encoding="utf-8"?>

  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:background="#b5555555">

  6. <LinearLayout

  7. android:layout_width="match_parent"

  8. android:layout_height="wrap_content"

  9. android:layout_alignParentBottom="true"

  10. android:background="#eee"

  11. android:orientation="vertical" >

  12. <EditText

  13. android:id="@+id/leaveword"

  14. android:layout_width="fill_parent"

  15. android:layout_height="wrap_content"

  16. android:layout_marginBottom="20dp"

  17. android:layout_marginLeft="20dp"

  18. android:layout_marginRight="20dp"

  19. android:layout_marginTop="20dp"

  20. android:gravity="top"

  21. android:hint="说点什么吧~"

  22. android:inputType="textMultiLine"

  23. android:lineSpacingExtra="6.0dp"

  24. android:maxHeight="150dp"

  25. android:minHeight="100dp"

  26. android:paddingLeft="10.0dp"

  27. android:paddingRight="10.0dp"

  28. android:paddingTop="10.0dp" />

  29. <LinearLayout

  30. android:layout_width="fill_parent"

  31. android:layout_height="wrap_content"

  32. android:layout_marginBottom="20dp"

  33. android:gravity="center"

  34. android:orientation="horizontal">

  35. <Button

  36. android:id="@+id/confirmButton"

  37. android:layout_width="80.0dip"

  38. android:layout_height="wrap_content"

  39. android:gravity="center"

  40. android:text="发表"

  41. android:textColor="#fff"

  42. android:textSize="16.0sp" />

  43. <Button

  44. android:id="@+id/cancleButton"

  45. android:layout_width="80.0dip"

  46. android:layout_height="wrap_content"

  47. android:layout_marginLeft="30dp"

  48. android:gravity="center"

  49. android:text="取消"

  50. android:textColor="#565656"

  51. android:textSize="16.0sp" />

  52. </LinearLayout>

  53. </LinearLayout>

  54. </RelativeLayout>

效果:

(Android)五分钟学会PopupWindow_JAVA

这样我们的界面就算完成了,下来我们来看一个Activity。写个了OpenView 来弹出view

 

[java] view plaincopyprint?

  1. public void OpenView() {

  2. LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

  3. popupWindowView = inflater.inflate(R.layout.popupwindow, null);

  4. popupWindow = newPopupWindow(popupWindowView,

  5. LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,true);

  6. // 设置PopupWindow的弹出和消失效果

  7. popupWindow.setAnimationStyle(R.style.popupAnimation);

  8. btnsure = (Button) popupWindowView.findViewById(R.id.confirmButton);

  9. btnsure.setOnClickListener(newButtonOnClickListener());

  10. cancleButton = (Button) popupWindowView.findViewById(R.id.cancleButton);

  11. cancleButton.setOnClickListener(new ButtonOnClickListener());

  12. leaveword = (EditText) popupWindowView.findViewById(R.id.leaveword);

  13. popupWindow.showAtLocation(btnsure, Gravity.CENTER, 0, 0);

  14. }


我们看到弹出来的有点动画效果,是因为我们在弹出时,加上了

[java] view plaincopyprint?

  1. popupWindow.setAnimationStyle(R.style.popupAnimation);

需要在styles.xml下加上

 

 

[html] view plaincopyprint?

  1. <style name="popupAnimation"parent="android:Animation">

  2. <itemname="android:windowEnterAnimation">@anim/in</item>

  3. <itemname="android:windowExitAnimation">@anim/out</item>

  4. lt;/style>


在 在工程res下新建anim文件夹,在anim文件夹先新建两个xml文件

 

in.xml

 

[html] view plaincopyprint?

  1. <?xml version="1.0"encoding="utf-8"?>

  2. <setxmlns:android="http://schemas.android.com/apk/res/android" >

  3.  

  4. <translate

  5. android:duration="1500"

  6. android:fromYDelta="5000"

  7. android:toYDelta="0" />

  8.  

  9. </set>


out.xml

 

 

[html] view plaincopyprint?

  1. <?xml version="1.0"encoding="utf-8"?>

  2. <setxmlns:android="http://schemas.android.com/apk/res/android">

  3. <translate

  4. android:fromYDelta="0"

  5. android:toYDelta="5000"

  6. android:duration="1500"

  7. />

  8. </set>


如果想要关闭弹出框

 

调用popupWindow.dismiss();就好

这样一个简单的PopupWindow例子就算完成了

 

[java] view plaincopyprint?

  1. public class MainActivity extendsActivity {

  2. private View popupWindowView;

  3. private PopupWindow popupWindow;

  4. private Button btnsure, cancleButton, btnOpen;

  5. private EditText leaveword;

  6.  

  7. @Override

  8. protected void onCreate(Bundle savedInstanceState) {

  9. super.onCreate(savedInstanceState);

  10. setContentView(R.layout.activity_main);

  11.  

  12. btnOpen = (Button) findViewById(R.id.btnOpen);

  13. btnOpen.setOnClickListener(newButtonOnClickListener());

  14. }

  15.  

  16. public void OpenView() {

  17. LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

  18. popupWindowView = inflater.inflate(R.layout.popupwindow, null);

  19. popupWindow = newPopupWindow(popupWindowView,

  20. LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,true);

  21. // 设置PopupWindow的弹出和消失效果

  22. popupWindow.setAnimationStyle(R.style.popupAnimation);

  23. btnsure = (Button) popupWindowView.findViewById(R.id.confirmButton);

  24. btnsure.setOnClickListener(newButtonOnClickListener());

  25. cancleButton = (Button) popupWindowView.findViewById(R.id.cancleButton);

  26. cancleButton.setOnClickListener(new ButtonOnClickListener());

  27. leaveword = (EditText) popupWindowView.findViewById(R.id.leaveword);

  28. popupWindow.showAtLocation(btnsure, Gravity.CENTER, 0, 0);

  29. }

  30.  

  31. private classButtonOnClickListenerimplements OnClickListener {

  32. @Override

  33. public void onClick(View vid) {

  34.  

  35. switch (vid.getId()) {

  36.  

  37. case R.id.btnOpen:

  38. OpenView();

  39. break;

  40. case R.id.confirmButton:

  41.  

  42. Toast.makeText(MainActivity.this, leaveword.getText().toString(), Toast.LENGTH_SHORT)

  43. .show();

  44.  

  45. break;

  46. case R.id.cancleButton:

  47. popupWindow.dismiss();

  48. break;

  49.  

  50. default:

  51. break;

  52. }

  53. }

  54. }

  55.  

  56. }

https://mp.weixin.qq.com/s/AzCA5ehDapYA7xsRoc5fjQ