zoukankan      html  css  js  c++  java
  • Android自定义Dialog(Loading)

    由于项目需要,要求UI要漂亮,不可避免的需要重写Android的一些默认组建,在此分享下重写Dialog 加载和 Dialog对话框的经历。

    先来说加载dialog(上面没有按钮,登陆和远程加载数据需要),Dialog对话框下次介绍。

    第一步:建立自定义Dialog布局页面:dialog_loading.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:gravity="center"
        android:layout_gravity="center"
        android:background="@drawable/dialog_bg"
        android:orientation="horizontal" >
    
        <ProgressBar
            style="?android:progressBarStyle"
            android:layout_width="27.0dip"
            android:layout_height="27.0dip"
            android:layout_gravity="center_vertical"
            android:indeterminateDrawable="@drawable/progress_refresh_rotate"
            android:visibility="visible" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10.0dip"
            android:text="@string/loading"
            android:textColor="#ff565656"
            android:textSize="18.0sp" />
    
    </LinearLayout>

    界面很简单,主要用到系统的ProgressBar,加入了自定义样式,即:android:indeterminateDrawable="@drawable/progress_refresh_rotate",

    progress_refresh_rotate是一个界面,设置一张图片后使用Android rotate旋转动画,实现加载图片资源的旋转。TextView就是在加载界面上的文字提示。

    progress_refresh_rotate.xml放于drawable目录下,如果没有该目录,自己建立即可。

    <?xml version="1.0" encoding="utf-8"?>
    <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:drawable="@drawable/blue_loading"
        android:duration="50"
        android:pivotX="50.0%"
        android:pivotY="50.0%" />

    第二步:重写系统Dialog,直接贴代码

    package com.antrou.wfs.dialog;
    
    import com.antrou.R;
    
    import android.app.Dialog;
    import android.content.Context;
    import android.os.Bundle;
    
    /**
     * 自定义加载进度Dialog
    * @author THL(HanleyTowne)
    * @date 2013-4-21 下午6:00:26
    *
     */
    public class LoadingProgress extends Dialog {
        
        
        public LoadingProgress(Context context, int theme){
            super(context, theme);
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dialog_loading);
        }
    
        
    }

    在OnCreate中指定刚刚写好的布局文件,需要注意的是上面提供了构造函数,提供了2个参数,其中theme参数是一个style主题,下面会介绍。

    第三步:在style.xml下添加Dialog主题样式

        
        <style name="MyDialog" parent="@android:style/Theme.Dialog">
            <item name="android:windowFrame">@null</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:windowIsTranslucent">false</item>
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:backgroundDimEnabled">false</item>
            <item name="android:windowContentOverlay">@null</item>
        </style>
        

    这里需要说明下 android:windowBackground 元素:值设置为: @android:color/transparent,如果是设置android:background属性,同样是@android:color/transparent,dialog

    是会有黑边的.所以请使用 android:windowBackground 属性。

    第四步:使用LoadingDialog

    Dialog dialog = new LoginProgress(XXXActivity.this, R.style.MyDialog);
    dialog.show();
    

    大功告成。放上用到的两张png,有没有感觉很眼熟,好吧,我承认是扒下QQ的图片。

  • 相关阅读:
    自主学习1
    动手动脑10.28
    《程序员修炼之道》阅读笔记三
    《程序员修炼之道》阅读笔记
    11月20,21,22训练赛
    Rabbits(跳兔子)
    01背包变式(并查集+dp01背包)
    问题 I: 约会序列(两边枚举)
    问题 J: 直角三角形
    Cow Exhibition (01背包的负数处理)
  • 原文地址:https://www.cnblogs.com/hanleytang/p/3041985.html
Copyright © 2011-2022 走看看