我正在 Android Jelly Beans 操作系统上创建一个警报对话框。一切正常,但我想要的是,我想要透明背景,而不是警报对话框的黑色背景。我在 stackoverflow 上读了很多文章和用户的问题,但没有一个能帮助我。
这是我的代码:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.CustomAlertDialog);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog1, int which) {
gActivityResult = REQUEST_PICK_CONTACT;
onResume();
return;
} });
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog1, int which) {
return;
} });
View view = getLayoutInflater().inflate(R.layout.alert_dialog, null);
builder.setTitle(R.string.gender_age);
builder.setInverseBackgroundForced(false);
builder.setView (view);
dialog = builder.create ();
这是我的 CustomAlertDialog,它在 res/values/styles.xml 中定义
<style name="CustomAlertDialog" parent="@android:style/Theme.Dialog">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<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_color</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:gravity">left</item>
</style>
这是颜色.xml
<resources>
<color name="transparent_color">#00000000</color>
</resources>
但这对我没有帮助。
我的问题:这可行吗?如果是,您能指导我正确的方向吗?
我发现这种方式与 AlertDialog.Builder 兼容。使用 Eclipse 中 Android“设备”选项卡中的“转储视图层次结构”按钮,我看到许多嵌套的 FrameLayout,看起来背景就在这些框架中,而不是窗口。
遍历这些视图并将每个视图的背景设置为透明(然后我的自定义视图漂浮在变暗的应用程序上,没有框架或背景,并且布局没有变化)。我有一个自定义视图,因此从它向下遍历,但从窗口向上遍历 ViewGroups 也应该可以。
我在我自己的 DialogFragment 子类中使用 AlertDialog.Builder 和自定义视图。
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
configureDialogBuilder(builder);
final LayoutInflater inflater = getActivity().getLayoutInflater();
customView = inflater.inflate(customViewId, null);
builder.setView(customView);
final AlertDialog dialog = builder.create();
return dialog;
}
@Override
public void onStart() {
super.onStart();
clearBackgrounds(customView);
}
private void clearBackgrounds(View view) {
while (view != null) {
view.setBackgroundResource(android.graphics.Color.TRANSPARENT);
final ViewParent parent = view.getParent();
if (parent instanceof View) {
view = (View) parent;
} else {
view = null;
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)