android - 如何在android中将透明背景设置为自定义对话框

标签 android dialog

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

示例代码:

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

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

示例截图:

enter image description here

如何避免这种 block 状颜色。

最佳答案

使用对话框代替 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 - 如何在android中将透明背景设置为自定义对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11864162/

相关文章:

java - 如何在每次触摸屏幕时绘制一个新的圆圈?

java - 使用 Canvas.drawText 从位图上的 TextView 获取错误的 Y 坐标

dialog - 如何调整 css-sprite 的部分大小?

Android 对话框 - 圆角和透明度

javascript - Jquery UI 对话框.. 在主页上加载对话框内容

android - Bing Maps API 与 Android 应用程序的集成情况如何?

Android kapt java.lang.UnsatisfiedLinkError 房间

android - 不向当前 Activity 堆栈添加新 Activity

java - 如何在对话框的列中显示结果?

java - Swing 中模态对话框的正确父级