歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android彈出窗口的實現(PopupWindow)

Android彈出窗口的實現(PopupWindow)

日期:2017/3/1 10:25:56   编辑:Linux編程

最近看到新浪微博頂部欄的微博分組效果很炫,從網上查了一些資料明白原來是用PopupWindow實現的,今天自己也寫了一個例子實現了這種效果,希望對大家有幫助。

PopupWindow就是彈出窗口的意思,類似windows下面的開始按鈕。PopupWindow可以實現浮層效果,而且可以自定義顯示位置,出現和退出時的動畫.

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

首先定義新浪微博的頂部欄,title_two_button.xml和main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content" >
  5. <TextView
  6. android:id="@+id/tvtitle"
  7. android:layout_width="fill_parent"
  8. android:layout_height="45dip"
  9. android:background="@drawable/search_bg"
  10. android:gravity="center"
  11. android:text="天堂沒有路"
  12. android:textColor="@color/black"
  13. android:textSize="25dip" />
  14. <Button
  15. android:id="@+id/btn_title_left"
  16. android:layout_width="50dip"
  17. android:layout_height="35dip"
  18. android:layout_gravity="center_vertical"
  19. android:background="@drawable/title_button"
  20. android:textColor="@color/white" />
  21. <Button
  22. android:id="@+id/btn_title_right"
  23. android:layout_width="50dip"
  24. android:layout_height="35dip"
  25. android:layout_gravity="right"
  26. android:layout_marginRight="2dip"
  27. android:layout_marginTop="4dip"
  28. android:background="@drawable/title_button2"
  29. android:textColor="@color/white" />
  30. </FrameLayout>
  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="@android:color/white"
  6. android:orientation="vertical" >
  7. <include
  8. android:id="@+id/top_title"
  9. layout="@layout/title_two_button" />
  10. </LinearLayout>
主要是為了實現一個這樣的界面:

Copyright © Linux教程網 All Rights Reserved