自定义ProgressDialog
来源:互联网 发布:在code.org学编程 编辑:程序博客网 时间:2024/06/05 22:31
今天在项目中用到了ProgressDialog,但是项目组长觉得样子有点丑,需要改写一下样式,于是就自定义了一个ProgressDialog,下面详细介绍下过程
效果图
首先打开AndroidSudio新建一个项目
MainActivity里面只有一个Button,作为点击按钮使用,点击之后就会显示我们要自定义的ProgressDialog
为Button添加点击事件
package com.example.hfs.customprogressdialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity { private Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mButton= (Button) this.findViewById(R.id.btn_test); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); }}
下面就是开始自定义我们的控件了
首先写下我们要用的的Style
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <!--自定义的Style--> <style name="common_dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowIsFloating">true</item> <!-- 设置未浮动窗口 --> <item name="android:windowFrame">@null</item> <!-- 设置无边框 --> <item name="android:windowNoTitle">true</item> <!-- 设置无标题 --> <item name="android:windowBackground">@android:color/transparent</item> <!-- 设置完全透明 --> <item name="android:backgroundDimEnabled">false</item> <!-- 设置屏幕变暗 --> </style> <style name="CustomDialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item> </style> <style name="CustomProgressDialog" parent="@style/CustomDialog"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowNoTitle">true</item> </style></resources>
然后是布局XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <ImageView android:id="@+id/loadingImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/progress_round"/> <TextView android:id="@+id/id_tv_loadingmsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textSize="20dp"/></LinearLayout>
接下来就是最主要的代码了
package com.example.hfs.customprogressdialog;import android.app.Dialog;import android.content.Context;import android.graphics.drawable.AnimationDrawable;import android.view.Gravity;import android.widget.ImageView;import android.widget.TextView;/** * Created by HFS on 2016/7/5. */public class CustomDialog extends Dialog { private Context context = null; private static CustomDialog customProgressDialog = null; public CustomDialog(Context context) { super(context); this.context = context; } public CustomDialog(Context context, int theme) { super(context, theme); } public static CustomDialog createDialog(Context context) { customProgressDialog = new CustomDialog(context, R.style.CustomProgressDialog); customProgressDialog.setCanceledOnTouchOutside(false); customProgressDialog.setContentView(R.layout.customprogressdialog); customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER; return customProgressDialog; } public void onWindowFocusChanged(boolean hasFocus) { if (customProgressDialog == null) { return; } ImageView imageView = (ImageView) customProgressDialog .findViewById(R.id.loadingImageView); AnimationDrawable animationDrawable = (AnimationDrawable) imageView .getBackground(); animationDrawable.start(); } public CustomDialog setTitile(String strTitle) { return customProgressDialog; } public CustomDialog setMessage(String strMessage) { TextView tvMsg = (TextView) customProgressDialog.findViewById(R.id.id_tv_loadingmsg); if (tvMsg != null) { tvMsg.setText(strMessage); } return customProgressDialog; }}
在MainActivity中写两个方法启动Dialog和停止Dialog
private void stopProgressDialog() { if (mCustomDialog != null) { mCustomDialog.dismiss(); mCustomDialog = null; } } private void startProgressDialog() { if (mCustomDialog == null) { mCustomDialog = CustomDialog.createDialog(this); mCustomDialog.setMessage("正在加载中..."); } mCustomDialog.show(); }
在MainActivity中的Button点击事件中start 2秒中后发送一个通知,stop
package com.example.hfs.customprogressdialog;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity { private Button mButton; private CustomDialog mCustomDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mButton = (Button) this.findViewById(R.id.btn_test); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startProgressDialog(); mHandler.sendEmptyMessageDelayed(0, 2000); } }); } private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); stopProgressDialog(); } }; private void startProgressDialog() { if (mCustomDialog == null) { mCustomDialog = CustomDialog.createDialog(this); mCustomDialog.setMessage("正在加载中..."); } mCustomDialog.show(); } private void stopProgressDialog() { if (mCustomDialog != null) { mCustomDialog.dismiss(); mCustomDialog = null; } }}
OK这样一个简单的自定义ProgressDialog就完成了
2 0
- 【progressDialog】自定义progressDialog
- 自定义ProgressDialog
- 自定义ProgressDialog
- 自定义progressdialog
- 自定义progressDialog
- 自定义ProgressDialog
- 自定义progressdialog
- 自定义progressDialog
- 自定义progressDialog~~~
- 自定义ProgressDialog
- 自定义ProgressDialog
- 自定义ProgressDialog
- 自定义ProgressDialog
- 自定义ProgressDialog
- 自定义progressdialog
- 自定义ProgressDialog
- 自定义ProgressDialog
- ProgressDialog自定义方法
- SQL语句总结
- 让IIS支持dat、apk等类型文件下载
- git学习二——多分支的git管理
- sql server 纯sql语句分页 存储过程 返回结果集及页总数等
- HTML5-表格元素
- 自定义ProgressDialog
- 原子访问(Atomic Access)
- Flask总结(一)
- 产品助理实习day2
- Spring AOP 代理对象的生成 part3
- 解析 JSON 格式数据
- 一段项目开发时候用的调试代码 二 --代码分析器(基于表驱动直接访问)
- 搜索
- SCU2016-01 O题dijkstra变形或者最小生成树变形