转:Android 对话框【Dialog】去除白色边框代码

使用样式文件,在values 目录下新建styles.xml文件,编写如下代码:

Xml代码 复制代码 收藏代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <style name="dialog" parent="@android:style/Theme.Dialog"><!--name是我们在使用时要用到的资源的标志,parent是指当前的样式所继承的父类样式-->
  4. <item name="android:windowFrame">@null</item>
  5. <item name="android:windowIsFloating">true</item>
  6. <item name="android:windowIsTranslucent">false</item>
  7. <item name="android:windowNoTitle">true</item><!--隐藏标题栏-->
  8. <item name="android:background">@color/clarity</item>
  9. <item name="android:windowBackground">@drawable/clarity</item><!--这儿也很重要啊,我这儿用了一张透明的.9.png的图,当然用#00000000也是可以的,否则的话这儿出来后有一个黑色的背景-->
  10. <item name="android:backgroundDimEnabled">false</item>
  11. </style>
  12. </resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="dialog" parent="@android:style/Theme.Dialog"><!--name是我们在使用时要用到的资源的标志,parent是指当前的样式所继承的父类样式--> <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:background">@color/clarity</item> <item name="android:windowBackground">@drawable/clarity</item><!--这儿也很重要啊,我这儿用了一张透明的.9.png的图,当然用#00000000也是可以的,否则的话这儿出来后有一个黑色的背景--> <item name="android:backgroundDimEnabled">false</item> </style> </resources>

去掉背景的方框使用以下代码:

Xml代码 复制代码 收藏代码

  1. <resources>
  2. <style name="dialog" parent="@android:style/Theme.Dialog">
  3. <item name="android:windowFrame">@null</item>
  4. <item name="android:windowIsFloating">true</item>
  5. <item name="android:windowIsTranslucent">false</item>
  6. <item name="android:windowNoTitle">true</item>
  7. <item name="android:background">@android:color/transparent</item>
  8. <item name="android:windowBackground">@android:color/transparent</item>
  9. <item name="android:backgroundDimEnabled">false</item>
  10. </style>
  11. </resources>
<resources> <style name="dialog" 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:background">@android:color/transparent</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:backgroundDimEnabled">false</item> </style> </resources>

调用时,使用AlerDialog的接口类,Dialog 接口编写如下代码:

Java代码 复制代码 收藏代码

  1. Dialog dialog = new Dialog(SetActivity.this, R.style.dialog);  
  2.                    dialog.setContentView(R.layout.test);  
  3.                    dialog.show(); 
Dialog dialog = new Dialog(SetActivity.this, R.style.dialog); dialog.setContentView(R.layout.test); dialog.show();

下面我们查看一下Dialog的源码文件,里面的构造函数为如下:

Java代码 复制代码 收藏代码

  1. public Dialog(Context context, int theme) {  
  2.         mContext = new ContextThemeWrapper(  
  3.             context, theme == 0 ? com.android.internal.R.style.Theme_Dialog : theme);  
  4.         mWindowManager = (WindowManager)context.getSystemService("window");  
  5.         Window w = PolicyManager.makeNewWindow(mContext);  
  6.         mWindow = w;  
  7.         w.setCallback(this);  
  8.         w.setWindowManager(mWindowManager, null, null);  
  9.         w.setGravity(Gravity.CENTER);  
  10.         mUiThread = Thread.currentThread();  
  11.         mDismissCancelHandler = new DismissCancelHandler(this);  
  12.     } 
public Dialog(Context context, int theme) { mContext = new ContextThemeWrapper( context, theme == 0 ? com.android.internal.R.style.Theme_Dialog : theme); mWindowManager = (WindowManager)context.getSystemService("window"); Window w = PolicyManager.makeNewWindow(mContext); mWindow = w; w.setCallback(this); w.setWindowManager(mWindowManager, null, null); w.setGravity(Gravity.CENTER); mUiThread = Thread.currentThread(); mDismissCancelHandler = new DismissCancelHandler(this); }

这里面我们可以看出,Android 使用了默认的构造函数为Dialog 设置样式,如果没有为其设置样式,即默认加载事先编写好的样式文件,Dialog 一共由多个9.png的图片构成,大部分都是带有边框的9.png图片,所以就是为什么我们上边的样式文件要将其背景去除掉。这个东西搞了我好久,希望对你有帮助

你可能感兴趣的:(android,目录,target,values,blank)