zoukankan      html  css  js  c++  java
  • android 自己定义dialog并实现失去焦点(背景透明)的功能

    前言:因为在项目中须要用到更新显示动画的需求,所以想到了dialog,自己定义dialog不难。网上教程非常多,可是在实现dialog背景透明的需求时,遇到了一点问题。网上的一些方法在我的机器上并没有实现,仅仅能曲折中找到了还有一个方法实现。尽管有点麻烦。但毕竟效果不错。

    此方法写在这里,一是和各位分享,二是做个记录,留待以后需求。

    不说了,上代码:

    以下是dialog自己定义布局文件,是运行任务用的,參考就可以。

    <?

    xml version="1.0" encoding="utf-8"?

    > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="运行中" android:textSize="18sp" /> <ImageView android:id="@+id/dialog_image" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center" /> </LinearLayout>


    自己定义dialog,运行任务的代码去掉了,能够加在onCreate()中。

    /**
     * 运行状态的对话框形式
     * @author Administrator
     *
     */
    class MyDialog extends Dialog{
    	ImageView imageView;
    
    	public MyDialog(Context context) {
    		super(context);
    		// TODO Auto-generated constructor stub
    	}
    	
    	public MyDialog(Context context,int theme) {
    		super(context, theme);
    		// TODO Auto-generated constructor stub
    	}
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		
    		//去掉activity标题
    		requestWindowFeature(Window.FEATURE_NO_TITLE);
    		
    		setContentView(R.layout.dialog);
            //设置标题
    		imageView = (ImageView)findViewById(R.id.dialog_image);
    	}
    }

    在实现之前。实验了dialog设置flag的方法,有人说能够,可是我并没有实现。也把代码留下,以作參考:

    //使dialog失去焦点
    			dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
    					WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    			dialog.setCanceledOnTouchOutside(true);

    真正实现的方法是使用style实现的,这个须要两个资源文件,一个color,一个style。

    <color name="transparent">#00000000</color>

    <style name="dialog" parent="@android:style/Theme.Dialog">
            <item name="android:windowFrame">@null</item><!--边框-->
            <item name="android:windowIsFloating">true</item><!--是否浮如今activity之上-->
            <item name="android:windowIsTranslucent">false</item><!--半透明-->
            <item name="android:windowNoTitle">true</item><!--无标题-->
            <item name="android:windowBackground">@color/transparent</item><!--背景透明-->
            <item name="android:backgroundDimEnabled">false</item><!--模糊-->
        </style>

    设置好资源文件之后。在调用时生成实例代码例如以下:

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

    至此完成。


    參考资料:http://www.cnblogs.com/windlivefamily/articles/2133956.html


  • 相关阅读:
    test
    Data mining with WEKA, Part 2: Classification and clustering
    MyISAM和InnoDB的区别
    SpringMVC源码剖析(一) 从抽象和接口说起
    数据库隔离级别详解
    spring MVC配置详解
    Spring单实例、多线程安全、事务解析
    mysql中int、bigint、smallint 和 tinyint的区别
    SpringMVC源码剖析(二) DispatcherServlet的前世今生
    SpringBoot与Lombok
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/6918841.html
Copyright © 2011-2022 走看看