android 自訂dialog並實現失去焦點(背景透明)的功能

來源:互聯網
上載者:User

標籤:背景   android   dialog   焦點   透明   

前言:由於在項目中需要用到更新顯示動畫的需求,所以想到了dialog,自訂dialog不難,網上教程很多,但是在實現dialog背景透明的需求時,遇到了一點問題,網上的一些方法在我的機器上並沒有實現,只能曲折中找到了另一個方法實現。雖然有點麻煩,但畢竟效果不錯。

此方法寫在這裡,一是和各位分享,二是做個記錄,留待以後需求。

不說了,上代碼:

下面是dialog自訂布局檔案,是執行任務用的,參考即可。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"     >    <TextView        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:text="執行中"        android:textSize="18sp"        />    <ImageView         android:id="@+id/dialog_image"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:layout_gravity="center"        />    </LinearLayout>


自訂dialog,執行任務的代碼去掉了,可以加在onCreate()中。

/** * 執行狀態的對話方塊形式 * @author Administrator * */class MyDialog extends Dialog{ImageView imageView;public MyDialog(Context context) {super(context);// TODO Auto-generated constructor stub}public MyDialog(Context context,int theme) {super(context, theme);// TODO Auto-generated constructor stub}@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);//去掉activity標題requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.dialog);        //設定標題imageView = (ImageView)findViewById(R.id.dialog_image);}}

在實現之前,實驗了dialog設定flag的方法,有人說可以,但是我並沒有實現。也把代碼留下,以作參考:

//使dialog失去焦點dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);dialog.setCanceledOnTouchOutside(true);

真正實現的方法是使用style實現的,這個需要兩個資源檔,一個color,一個style。

<color name="transparent">#00000000</color>

<style name="dialog" parent="@android:style/Theme.Dialog">        <item name="android:windowFrame">@null</item><!--邊框-->        <item name="android:windowIsFloating">true</item><!--是否浮現在activity之上-->        <item name="android:windowIsTranslucent">false</item><!--半透明-->        <item name="android:windowNoTitle">true</item><!--無標題-->        <item name="android:windowBackground">@color/transparent</item><!--背景透明-->        <item name="android:backgroundDimEnabled">false</item><!--模糊-->    </style>

設定好資源檔之後,在調用時產生執行個體代碼如下:

MyDialog dialog = new MyDialog(this,R.style.dialog);dialog.show();

至此完畢。


參考資料:http://www.cnblogs.com/windlivefamily/articles/2133956.html


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

android 自訂dialog並實現失去焦點(背景透明)的功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.