问题描述
我最初的目标是一个模态对话框,但你知道,Android 不支持这种类型的对话框.或者,构建一个以对话为主题的活动可能会奏效.
这是代码,
My original goal here was a modal dialog, but you know, Android didn't support this type of dialog. Alternatively, building a dialog-themed activity would possibly work.
Here's code,
public class DialogActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme_Dialog);
super.onCreate(savedInstanceState);
requestWindowFeature (Window.FEATURE_NO_TITLE);
Button yesBtn = new Button(this);
yesBtn.setText("Yes");
yesBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DialogActivity.this.finish();
}
});
this.setContentView(yesBtn);
}
然而,背景总是黑色的,这真的很混乱.人们遇到了同样的问题,
http://code.google.com/p/android/issues/detail?id=4394
我只是想让它看起来更像用户对话.那么,如何让这个 DialogActivity 透明并且从不覆盖底层屏幕?
谢谢.
However, the background was always black, that really confusing. People got same problem,
http://code.google.com/p/android/issues/detail?id=4394
I just wanna make it look more dialog-like for user. So, how to get this DialogActivity transparent and never cover underlying screen?
Thanks.
这就是我创建样式的方式
This was how I created the style
<style name="CustomDialogTheme" parent="android:Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">false</item>
</style>
推荐答案
你可以通过这个创建一个透明的对话框.
You can create a tranparent dialog by this.
public class DialogActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle(" ");
alertDialog.setMessage("");
alertDialog.setIcon(R.drawable.icon);
alertDialog.setButton("Accept", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.setButton2("Deny", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
}
在此之后,只需在 AndroidManifest.xml 中添加一行,在清单中定义您的活动.
After this just put a line in AndroidManifest.xml, where you define your activity in manifest.
android:theme="@android:style/Theme.Translucent.NoTitleBar"
这篇关于Android:如何创建透明的对话框主题活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!