Android創建自定義dialog方法詳解-樣式去掉陰影效果


在自定義組件時,從已有組件源碼中會很大收獲。
就拿progressDialog來說
     間接父類是dialog,想了解dialog繼承結構可以去百度,或者
    從構造器來說ProgressDialog(Context context, int theme)很明顯需要個樣式主題文件,我們可以在value文件下自定義一個樣式文件。
   從外觀上需要個動態效果控件和文本框兩個屬性
    ProgressBar mProgress;   TextView mMessageView
源碼中onCreate()方法中 有
     View view = inflater.inflate(R.layout.alert_dialog_progress, null);//來加載布局文件
     setView(view);
    動態效果是由ProgressBar實現,當然我們可以通過給圖片添加動態效果也可以實現類似功能,這就需要個anim文件
從上可以總結出創建自定義dialog需要的步驟
 1.繼承dialog
 2.一個主題樣式文件
 3.一個布局文件來加載
 4.一個anim文件

代碼:public class IphoneProgersssDialog extends Dialog {
   private Context context;
   private ImageView img;
   private TextView txt;
       public IphoneProgersssDialog(Context context) {
               super(context, R.style.iphone_progress_dialog);
               this.context=context;
               //加載布局文件
               LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
               View  view=inflater.inflate(R.layout.iphone_progress_dialog, null); 
               img=(ImageView) view.findViewById(R.id.iphone_progress_dialog_img);
               txt=(TextView) view.findViewById(R.id.iphone_progress_dialog_txt);
               //給圖片添加動態效果
               Animation anim=AnimationUtils.loadAnimation(context, R.anim.progressbar);
               img.setAnimation(anim);
               txt.setText(R.string.iphone_progressbar_dialog_txt);
               //dialog添加視圖
               setContentView(view);

       }

       public void setMsg(String msg){
               txt.setText(msg);
       }
       public void setMsg(int msgId){
               txt.setText(msgId);
       }

}

 

看了下pregeressdialog中像activity類一樣都有生命周期函數,其實dialog和activity都是窗體概念並不是api中window類,有個地方還是不明白,希望哪位以后版主有針對setcontentview 和setview的話題。
  現在我們就做個iphone風格的進度條吧!

1.繼承
    public class IphoneProgersssDialog extends Dialog 
2.定義主題樣式 
   在value資源文件下
    <style name="iphone_progress_dialog" parent="@android:style/Theme.Dialog">
     <item name="android:windowFrame">@null</item> <!--Dialog的windowFrame框為無--> 
     <item name="android:windowIsFloating">true</item><!-- 是否漂現在activity上-->
     <item name="android:windowIsTranslucent">true</item><!-- 是否半透明 -->
     <item name="android:windowNoTitle">true</item>
     <item name="android:backgroundDimEnabled">false</item><!-- dim:模糊的 陰影效果 -->
     <item name="android:windowBackground">@drawable/load_bg</item><!-- 背景圖片的大小也影響窗口的大小 -->
   </style>

在構造器中   super(context, R.style.iphone_progress_dialog);
3.定義動畫文件  progressbar.xml
  <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
   android:shareInterpolator="false" >
<rotate 
       android:interpolator="@android:anim/linear_interpolator"
       android:pivotX="50%"
       android:pivotY="50%"
       android:fromDegrees="0"
       android:toDegrees="+360"
       android:duration="1000"
       android:startOffset="-1"
       android:repeatMode="restart"
       android:repeatCount="-1"/>


</set>
給圖片加載動畫
Animation anim=AnimationUtils.loadAnimation(context, R.anim.progressbar);
 img.setAnimation(anim);


最后就是setContentview;沒有去分析setView的區別

圖片不曉得我只上傳不了,也許是瀏覽器的原因吧。

   使用方法:

  new IphoneProgersssDialog ().show();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM