[Android界面] 如何 去掉dialog的黑色背景和边框 DEMO

android系统的默认对话框是黑色背景,白色边框的样式,对于android系统来说是相当漂亮的,可是与自己的项目风格不搭,所以只好想办法重写他的样式了,当然dialog是支持样式重写的

使用new Dialog(context, style).setContentView(layout);

即可定制属于自己项目的dialog

当然,这儿的style起到了至关重要的作用,要知道那个与项目不搭的白边得全靠它了

在value下新建style.xml

<?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>


下面分享一下我的代码:


首先:MainActivity

protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
}
   
   public void dialogStyle(View v){
	  final Dialog d=new Dialog(this, R.style.dialog);
	   View vv = LayoutInflater.from(this).inflate(R.layout.dialog_test, null);
	   TextView delete = (TextView) vv.findViewById(R.id.delete);
	   
	   d.setCanceledOnTouchOutside(true);
	   d.setContentView(vv);
	   d.show();
	   
	   delete.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			d.dismiss();
		}
	});
   }
}

然后:style


   <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">@null</item>  
<item name="android:backgroundDimEnabled">false</item>  
<item name="android:windowBackground"> @android:color/transparent </item>
</style>  

最后xml

<?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="vertical" >
    
    
    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
    
    <TextView 
        android:id="@+id/delete"
        android:layout_width="238dp"
        android:layout_height="58dp"
        android:background="#2c2c2c"
        android:text="删除联系人"
        android:textColor="#ffffff"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        />
</RelativeLayout>
</LinearLayout>



源码地址:http://download.csdn.net/detail/u014608640/8046705   有需要的朋友可以下载


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值