在自定义组件时,从已有组件源码中会很大收获。
就拿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();

最新文章

  1. 用SSH访问内网主机的方法
  2. scikit-learn 线性回归算法库小结
  3. 在树莓派上部署InfoPi
  4. 在Win7 64位注册ActiveX控件
  5. mac os 中类似于Linux的yum工具,或ubuntu的apt-get工具Homebrew
  6. Flume practices and sqoop hive 2 oracle
  7. Java虚拟机支持的最大内存限制
  8. hdu2021(很闲~~)
  9. InnoDB和MyISAM存储引擎的区别
  10. QTP特点有哪些?
  11. openvpn实现内网 映射到 外网
  12. JAVA 遍历数组
  13. Go语言之Interface(二)
  14. dict()的另一种用法
  15. java内存模型与volatile变量与Atomic的compareAndSet
  16. linux下关于PCL(point cloud library)库的安装,三行命令错误的问题
  17. SQL Server 中系统视图sysobjects中type字段的说明
  18. win10桌面显示我的电脑设置
  19. 解决使用Foxmail客户端软件不能收取腾讯企业邮箱的全部邮件
  20. Code Signal_练习题_shapeArea

热门文章

  1. 1. Netty解决Tcp粘包拆包
  2. (WF)
  3. python3 写文件
  4. 单元测试(junit使用)
  5. ArrayList源码
  6. Apache HttpClient
  7. 补一篇关于Jackson和Gson的文章
  8. PLSQL_批量压缩表Table Compress(案例)
  9. VMware和CentOS7安装和配置
  10. c++学习-数组