如何在Android中将透明背景设置为自定义对话框 [英] How to set Transparent Background as a Custom Dialog Box in android

查看:140
本文介绍了如何在Android中将透明背景设置为自定义对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将自定义对话框设置为透明。

I need to make my custom dialog box as a transparent.

示例代码:

Dialog dialog;
@Override
protected Dialog onCreateDialog(int id) 
{ 
    switch(id) 
    {
        case 1:
            AlertDialog.Builder builder = null;
            Context mContext = this;
            LayoutInflater inflater = ( LayoutInflater ) mContext.getSystemService( LAYOUT_INFLATER_SERVICE );
            View layout = inflater.inflate( R.layout.about_page, ( ViewGroup ) findViewById( R.id.about_Root ) );
            builder = new AlertDialog.Builder( mContext );
            builder.setView( layout );
            dialog = builder.create();
            break;
    }
    return dialog;
}

如何设置透明对话框。

我的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/about_Root"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/alert_page_border"
    android:orientation="vertical" >        
    <TextView 
        android:id="@+id/about_Title"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:gravity="center"
        android:layout_weight="1"
        android:textSize="25dip"
        android:textColor="@android:color/white"
        android:textStyle="bold|italic"
        android:text="Welcome" />
</LinearLayout>

代码Alert_page_border.xml

Code Alert_page_border.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">     
    <solid android:color="#3b3b3b" />
    <stroke 
        android:width="3dp"
        android:color="#ffffff" />
    <padding 
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" /> 
    <corners 
        android:radius="2dp" />     
</shape>

示例屏幕截图:

如何避免这种块状颜色。

How do avoid this block color.

推荐答案

使用对话框代替AlertDialog

Use dialog instead of AlertDialog

final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(R.layout.splash);
dialog.show();

这篇关于如何在Android中将透明背景设置为自定义对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆