簡體   English   中英

如何在android中將透明背景設置為自定義對話框

[英]How to set Transparent Background as a Custom Dialog Box in android

我需要將自定義對話框設置為透明。

示例代碼:

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>

示例截圖:

在此輸入圖像描述

如何避免這種塊顏色。

使用對話框而不是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();

如果您使用API​​> = 11,則可以嘗試將對話框設置為對話框:

new AlertDialog.Builder(mContext , android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

或者您可以嘗試將背景設置為透明:

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));

你的這個問題准確描述了我的問題。 我嘗試了在這個線程中偶然發現的每一個解決方案以及更多的線程。 沒有任何效果。 最后我偶然發現了這個帖子相應的答案

我不得不創建一個自定義對話框類。 所以我做了,它有效。 實際上以下樣式對我來說足夠了:

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="transparent_black">#66000000</color>
</resources>

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="custom_dialog_theme" parent="@android:style/Theme.Translucent">
        <item name="android:windowBackground">@color/transparent_black</item>
        <item name="android:windowIsFloating">true</item>
    </style>
</resources>

對話框類的代碼:

import android.app.Dialog;
import android.content.Context;

import my.app.com.android.R;

public class CustomDialog extends Dialog {
    public AddBookCustomDialog(final Context context) {
        super(context, R.style.custom_dialog_theme);
        this.setContentView(R.layout.add_book_dialog);
    }
}

以及我如何在活動代碼中使用此對話框:

CustomDialog customDialog = new CustomDialog(this);
customDialog.show();

我不知道為什么我必須創建一個自定義類才能將主題考慮在內。 現在我已經實現了我想要做的事:透明對話。 只要我不再使用AlertDialog.Builder的便捷方法, AlertDialog.Builder就是定位正面和負面按鈕。 希望我的代碼能為您提供幫助。

另請注意alpha的低值 - 甚至可以嘗試使用00,以確保您的配置是否有效。

使用dialog.getWindow().setBackgroundDrawable(null)刪除默認背景。

以下是供參考的文檔:

 /*** Set the background to a given Drawable, or remove the background. If the * background has padding, this View's padding is set to the background's * padding. However, when a background is removed, this View's padding isn't * touched. If setting the padding is desired, please use * {@link #setPadding(int, int, int, int)}. * * @param d The Drawable to use as the background, or null to remove the * background */ 
 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/a"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:background="#00000000">

 <!-- Write your LinearLayout code here -->

 </RelativeLayout>

試試這個代碼。

試試這個,

layout.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

在此行之后添加上面的代碼。

View layout = inflater.inflate( R.layout.about_page, ( ViewGroup ) findViewById( R.id.about_Root ) );

只需創建一個樣式並將其應用於對話框

    <style name="myDialogTheme" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@color/Transparent</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>

然后,使用此主題創建對話框;

    Dialog myDialog = new Dialog(this,R.style.myDialogTheme) {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.about_page);
        }
    };

希望這能幫助您實現目標。

只是刪除

來自Alert_page_border.xml

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM