Android 透明 AlertDialog 有黑色背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25174165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transparent AlertDialog has black background
提问by superuserdo
I have a custom AlertDialog
style that makes the AlertDialog
box transparent. It works fine except that when I inflate my desired transparent layout into the alert dialog window, it shows up with a black background. My goal is to have a completely transparent AlertDialog
to where it seems as if there are only 4 buttons floating rather than a frame.
Image one is what the custom dialog is giving me, image two is my desired or what I am aiming for.
我有一个AlertDialog
使AlertDialog
盒子透明的自定义样式。它工作正常,只是当我将所需的透明布局膨胀到警报对话框窗口中时,它显示为黑色背景。我的目标是让一个完全透明AlertDialog
的地方看起来好像只有 4 个按钮浮动而不是框架。图一是自定义对话框给我的,图二是我想要的或我的目标。
Here is the code to the custom Dialog
这是自定义的代码 Dialog
<style name="CustomDialog" parent="android:Theme.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:backgroundDimEnabled">false</item>
<item name="android:background">@android:color/transparent</item>
</style>
Here is what I am calling in my onCreate()
这是我在我的 onCreate()
AlertDialog.Builder imageDialog = new AlertDialog.Builder(TimeLine.this, R.style.CustomDialog);
inflater = getLayoutInflater();
View view=inflater.inflate(R.layout.tabs, null);![enter image description here][1]
AlertDialog a = imageDialog.create();
a.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
a.setView(view, 0, 0, 0, 0);
a.show();
*Edit**** **The code for the tabs layout xml is here`
*编辑**** **标签布局xml的代码在这里`
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="@+id/tabLayout"
android:background="#000000ff">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_marginTop="206dp"
android:layout_below="@+id/button4"
android:layout_alignStart="@+id/button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button3"
android:layout_alignTop="@+id/button2"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button4"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="100dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="100dp" />
</RelativeLayout>
`
`
From testing to see what the source of the problem is, I have found out that it is true that the layout is transparent because when I change the background color of the layout, so does the alert dialog change. However when the layout is set to transparent, it seems that what is behind the inflated layout is black. And because of that I am unsure of what to do or whether it is the AlertDialog
settings or my layout code.
通过测试查看问题的根源是什么,我发现布局是透明的,因为当我更改布局的背景颜色时,警报对话框也会更改。但是当布局设置为透明时,看起来膨胀的布局后面是黑色的。正因为如此,我不确定要做什么,或者是AlertDialog
设置还是我的布局代码。
回答by Rod_Algonquin
The problem is that AlertDialog builder
is actually not good for designing transparent dialog and will and always have this black background which is actually a Theme for it, instead use the Dialog
to create a transparent theme instead.
问题是这AlertDialog builder
实际上并不适合设计透明对话框,并且会并且始终使用黑色背景,这实际上是它的主题,而是使用Dialog
来创建透明主题。
sample:
样本:
Dialog alertDialog = new Dialog(this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.tabs);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
Using Dialog
does not require any theme manipulation for transparent background so it is basically easy.
使用Dialog
透明背景不需要任何主题操作,因此基本上很容易。
回答by Michael Shrestha
Have you tried using something like this:
你有没有试过使用这样的东西:
<style name="CustomDialog" parent="@android:style/Theme.Translucent">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
...
</style>
EDIT
编辑
Try this in java code
在java代码中试试这个
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
回答by Mehul Joisar
In your R.layout.tabs
,
在你的R.layout.tabs
,
Replace
代替
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="@+id/tabLayout"
android:background="#000000ff">
with
和
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="@+id/tabLayout"
android:background="@android:color/transparent">
回答by vikas kumar
Those who are having still problem creating custom transparent dialog or alert dialog, can use this combination. I also wanted to show custom background this is what worked for me.
那些仍然在创建自定义透明对话框或警报对话框时遇到问题的人,可以使用这种组合。我还想显示自定义背景,这对我有用。
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
example:-
例子:-
/**
* alert dialog
*/
public class ToolTipView extends AlertDialog {
public ToolTipView(@NonNull Context context) {
super(context);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_tool_tip_view);
}
}