標籤:背景 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並實現失去焦點(背景透明)的功能