用Dialog實現的主動彈出式氣泡

來源:互聯網
上載者:User

       最近項目中需要做一個異常處理,就是在使用者上次異常退出程式後下次進入時給出一個氣泡提示。網上有很多氣泡的實現方式是基於地圖開發時的,也有是基於popwindow來做的,基於map做的是不適用於我這種情況的,開始先用popwindow做了一個,但發現它只能被動的調出。不滿足當前需求。通過研究自己用dialog實現了一個主動彈出的氣泡,與大家分享下~~先哈~~無圖無真相~~

實現思路:

這個主要就是給Dialog定義了一個樣式,然後利用TextView的setMinWidth()和setMaxWidth()來限制彈出氣泡的大小,再通過擷取Dialog所在window來設定它的LayoutParams中的x y座標來控制在螢幕中顯示的位置。

package com.ayj.bubble;import android.app.Activity;import android.app.Dialog;import android.os.Bundle;import android.text.Html;import android.util.DisplayMetrics;import android.view.View;import android.view.Window;import android.view.WindowManager.LayoutParams;import android.widget.TextView;public class TestBubbleActivity extends Activity {/** 全域螢幕的高和寬 */private static int SCREEN_WIDTH = 0 ;private static int SCREEN_HEIGHT = 0;/**氣泡view*/    private View bubbleView = null;    /**氣泡dialog*/    private Dialog bubbleAlert = null;        /**我知道了*/private TextView tvKnow = null;/**氣泡顯示內容*/private TextView tvBubContent = null;        public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                getDimension();                bubbleView = getLayoutInflater().inflate(R.layout.overlay_pop, null);tvKnow = (TextView)bubbleView.findViewById(R.id.bubble_btn);tvKnow.setText(Html.fromHtml("<u>"+"我知道了"+"</u>"));tvBubContent = (TextView)bubbleView.findViewById(R.id.bubble_text);tvBubContent.setText("上次程式異常退出,正在傳輸曆史資料...");tvKnow.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v) {bubbleAlert.cancel();}});int tmpWidth = SCREEN_WIDTH/5*3;int tmpHeight =SCREEN_HEIGHT/8;System.out.println("tmpWidth=****=" + tmpWidth);System.out.println("tmpHeight=++++=" + tmpHeight);//設定TextView寬度tvKnow.setMinWidth(tmpWidth);tvBubContent.setMaxWidth(tmpWidth);//以指定的樣式初始化dialogbubbleAlert = new Dialog(this,R.style.bubble_dialog);        Window win = bubbleAlert.getWindow();//擷取所在windowLayoutParams params = win.getAttributes();//擷取LayoutParamsparams.x = -(SCREEN_WIDTH/8);//設定x座標params.y = -tmpHeight;//設定y座標params.width = tmpWidth;win.setAttributes(params);//設定生效bubbleAlert.setCancelable(false);bubbleAlert.setContentView(bubbleView);bubbleAlert.show();    }    /** * 擷取螢幕尺寸 */private void getDimension(){/** 擷取螢幕的寬和高 */DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);SCREEN_WIDTH = dm.widthPixels;SCREEN_HEIGHT = dm.heightPixels;}    }


Style檔案:

<?xml version="1.0" encoding="utf-8"?><resources><style name="bubble_dialog" parent="@android:style/Theme.Dialog">        <item name="android:windowFrame">@null</item>        <item name="android:windowIsFloating">true</item>        <item name="android:windowIsTranslucent">false</item>        <item name="android:windowNoTitle">true</item>        <item name="android:background">@android:color/transparent</item>        <item name="android:windowBackground">@android:color/transparent</item>        <item name="android:backgroundDimEnabled">false</item></style></resources>

這裡只貼出核心代碼了,其他代碼自己可以下載原始碼研究下。

歡迎大家多多交流。

提供原始碼下載 :點擊開啟連結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.