自定义ProgressDialog实现加载框效果

   之前一段时间做了一个效果用alertDialog弹出一个带有progressbar的效果的加载框,发现我不管设置,弹出加载界面大小的宽度都有固定宽度的留白,应该是progressbar引起,所以换成自定义的progressDialog实现加载框并且去除宽度空白效果。

                                                

public class CustomDialog extends ProgressDialog{
    public CustomDialog(Context context) {
        super(context);
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        init(getContext());
    }

    private void init(Context context) {
        //设置不可取消,点击其他区域不能取消,实际中可以抽出去封装供外包设置
         setCancelable(false);
        setCanceledOnTouchOutside(false);
        setContentView(R.layout.loading_progressbar);
         WindowManager.LayoutParams params = getWindow().getAttributes();
         params.width = WindowManager.LayoutParams.WRAP_CONTENT;
         params.height = WindowManager.LayoutParams.WRAP_CONTENT;
         getWindow().setAttributes(params);
        }
       @Override public void show()
       {
           super.show();
       }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/loadig_root_rl"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerInParent="true"
    android:background="@drawable/shap_dia_bg"
    >

    <LinearLayout
        android:layout_centerInParent="true"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@android:color/transparent">
        <ProgressBar
            android:id="@+id/pb_loading"
            android:layout_width="65dp"
            android:layout_height="65dp"
            android:layout_centerInParent="true"
            android:background="@android:color/transparent"
            />
        <TextView
            android:id="@+id/message"
            android:text="努力加载中..."
            android:textSize="12sp"
            android:layout_marginTop="5dp"
            android:textColor="#f4f7ed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            />
    </LinearLayout>

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="8dp"/>
    <solid android:color="#88000000"/>
</shape>

好了,到目前位置,如果按照上面方法实现加载框就会出现宽度会出现留白,那如何解决这个bug,看下面解决办法。

代码写完,演示效果的时候,我们发现对话框度出现了问题:在按照5.0以上系统左右俩边留白了,对话框实际宽度大于我们布局宽度

1.解决宽度留白问题。

首先我们自定义style,继承于Theme.AppCompat.Dialog,重写俩个属性,其中

android:backgroundDimEnabled:表示页面时候变暗,我们设置false,不变暗
android:windowBackground:表示背景颜色,我们这种为透明,因为我们给布局文件背景设置了圆角,如果不设置对话框为透明的话,圆角部分会留白
创建好自定义样式后,我们只要在创建对话框的时候调用另外重载构造方法设置样式就可以
new CustomDialog(this, R.style.CustomDialog)解决宽度不适配问题。
 

2.修改颜色

 <style name="CustomDialog"
parent="AppTheme">
<item name="android:backgroundDimEnabled">false</item> 
<item name="android:windowBackground">@android:color/transparent</item>
 <item name="colorAccent">#ffE91E63</item> </style>
这里需要注意的是使用 colorAccent兼容低版本,而不是android:colorAccent



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值