android custom dialog background

落花浮王杯 提交于 2019-12-13 12:04:28

问题


I need to show a custom dialog in my Android application. Standard AlertDialog design is unacceptable. Android docs say:

Tip: If you want a custom dialog, you can instead display an Activity as a dialog instead of using the Dialog APIs. Simply create an activity and set its theme to Theme.Holo.Dialog in the manifest element:

That's it. The activity now displays in a dialog window instead of fullscreen.

Sounded promising. I did it, but transparency does not work! Background is always grey:

Here is a desc in the manifest:

<activity
    android:name=".MyDialogActivity"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Holo.Dialog.NoActionBar" >
</activity>

Here is a root of my activity:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#00000000"
    android:layout_width="400dp"
    android:layout_height="wrap_content" >

    // Content ... //

</RelativeLayout>

I tried also android:background="@null" - effect was the same. If I use android:background="#ff0000" then it's red (as it should be). But how do I make it transparent?

Update

I ended up with

<style name="MyThemeDialogCustom" parent="android:Theme.Holo.Dialog.NoActionBar" >
    <item name="android:windowBackground">@color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
</style>

回答1:


make theme like this

<style name="ThemeDialogCustom">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowBackground">@color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
</style>



回答2:


Try...

You can avoid the gray background like this, Here I did get transparent background.

    ........
    Dialog dialog = new Dialog(MainActivity.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    Window window = dialog.getWindow();
    window.setBackgroundDrawableResource(android.R.color.transparent);
    dialog.setContentView(R.layout.popup_layout);
    Button dialogBtn= (Button) dialog.findViewById(R.id.btn);

    dialogBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //task
        }
    });
    dialog.show();
    .......



回答3:


dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);



回答4:


dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);



回答5:


i have used this as a style:

<style name="DialogTransparent" parent="Theme.AppCompat.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowTitleStyle">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>

    <item name="android:background">@android:color/transparent</item>
</style>
<!-- <item name="android:backgroundDimEnabled">false</item> -->

it has also background transparent, setting the brackgroundDimEnabled parameter to false works for not showing the gray background that shows with the dialog in his back.



来源:https://stackoverflow.com/questions/13669450/android-custom-dialog-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!