歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android UI進階之彈窗的使用(2)--實現通訊錄的彈窗效果

Android UI進階之彈窗的使用(2)--實現通訊錄的彈窗效果

日期:2017/3/1 10:37:50   编辑:Linux編程

相信大家都體驗過Android通訊錄中的彈窗效果。

相關閱讀:Android UI進階之彈窗的使用 http://www.linuxidc.com/Linux/2012-01/52254.htm

如圖所示:


android中提供了QuickContactBadge來實現這一效果。這裡簡單演示下。

首先創建布局文件:

[html]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <QuickContactBadge
  8. android:id="@+id/badge"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:src="@drawable/icon">
  12. </QuickContactBadge>
  13. </LinearLayout>

很簡單,在布局中添加一個QuickContactBadge組件即可。

在Activity中配置:

[java]
  1. public class QuickcontactActivity extends Activity {
  2. /** Called when the activity is first created. */
  3. @Override
  4. public void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7. QuickContactBadge smallBadge = (QuickContactBadge) findViewById(R.id.badge);
  8. // 從email關聯一個contact
  9. smallBadge.assignContactFromEmail("[email protected]", true);
  10. // 設置窗口模式
  11. smallBadge.setMode(ContactsContract.QuickContact.MODE_SMALL);
  12. }
  13. }

注意加入讀通訊錄的權限

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>  

實現效果如圖:


但是這個組件局限性很大,彈出窗口中只能是一些contact操作。但是仔細一想,這樣的操作並不難,不就是一個帶動畫的彈窗麼。下面就來我們自己實現一個。

實現一個帶動畫的彈窗並不難,在我的之前一篇博客中有講過彈窗PopupWindow的使用,不清楚彈窗的朋友可以去看下。在這裡實現的難點主要有這些:

1.判斷基准view在屏幕中的位置,從而確定彈窗彈出的位置以及動畫。這是非常重要的一點,或許基准在屏幕上方,那麼就要向下彈出。

2.動態的添加彈窗中的按鈕,並實現點擊

3.箭頭位置的控制。箭頭應該保持在基准的下方。

4.動畫的匹配。裡面有兩種動畫。一種是PopupWindow彈出動畫,我們通過設置彈窗的style來實現(style的用法可以參考我之前的博客)。另一種是彈窗中間的布局的動畫。

  了解了難點以後,寫起來就方便了。

首先實現彈窗的布局:

[html]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content">
  5. <FrameLayout
  6. android:layout_marginTop="10dip"
  7. android:id="@+id/header2"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:background="@drawable/quickcontact_top_frame"/>
  11. <ImageView
  12. android:id="@+id/arrow_up"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:src="@drawable/quickcontact_arrow_up" />
  16. <HorizontalScrollView
  17. android:id="@+id/scroll"
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:fadingEdgeLength="0dip"
  21. android:layout_below="@id/header2"
  22. android:background="@drawable/quickcontact_slider_background"
  23. android:scrollbars="none">
  24. <LinearLayout
  25. android:id="@+id/tracks"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:paddingTop="4dip"
  29. android:paddingBottom="4dip"
  30. android:orientation="horizontal">
  31. <ImageView
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:src="@drawable/quickcontact_slider_grip_left" />
  35. <ImageView
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:src="@drawable/quickcontact_slider_grip_right" />
  39. </LinearLayout>
  40. </HorizontalScrollView>
  41. <FrameLayout
  42. android:id="@+id/footer"
  43. android:layout_width="fill_parent"
  44. android:layout_height="wrap_content"
  45. android:layout_below="@id/scroll"
  46. android:background="@drawable/quickcontact_bottom_frame" />
  47. <ImageView
  48. android:id="@+id/arrow_down"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_marginTop="-1dip"
  52. android:layout_below="@id/footer"
  53. android:src="@drawable/quickcontact_arrow_down" />
  54. </RelativeLayout>

窗體內部使用一個HorizontalScrollView可以實現一個滑動效果。我們可以動態的在這個布局中添加按鈕,我們稱作Actionitem。

  寫一個ActionItem類,使得我們可以用一個ArrayList做容器,動態的添加這些actionitem。這些都是服務於第二個難點。

[java]
  1. package com.notice.quickaction;
  2. import android.graphics.drawable.Drawable;
  3. import android.view.View.OnClickListener;
  4. /**
  5. * Action item, 每個item裡面都有一個ImageView和一個TextView
  6. */
  7. public class ActionItem {
  8. private Drawable icon;
  9. private String title;
  10. private OnClickListener listener;
  11. /**
  12. * 構造器
  13. */
  14. public ActionItem() {
  15. }
  16. /**
  17. * 帶Drawable參數的構造器
  18. */
  19. public ActionItem(Drawable icon) {
  20. this.icon = icon;
  21. }
  22. /**
  23. * 設置標題
  24. */
  25. public void setTitle(String title) {
  26. this.title = title;
  27. }
  28. /**
  29. * 獲得標題
  30. *
  31. * @return action title
  32. */
  33. public String getTitle() {
  34. return this.title;
  35. }
  36. /**
  37. * 設置圖標
  38. */
  39. public void setIcon(Drawable icon) {
  40. this.icon = icon;
  41. }
  42. /**
  43. * 獲得圖標
  44. */
  45. public Drawable getIcon() {
  46. return this.icon;
  47. }
  48. /**
  49. * 綁定監聽器
  50. */
  51. public void setOnClickListener(OnClickListener listener) {
  52. this.listener = listener;
  53. }
  54. /**
  55. * 獲得監聽器
  56. */
  57. public OnClickListener getListener() {
  58. return this.listener;
  59. }
  60. }
Copyright © Linux教程網 All Rights Reserved