Raj*_*Raj 3 android android-layout android-alertdialog android-4.2-jelly-bean
我正在Android Jelly Beans OS上创建一个警告对话框.一切都运作良好,但我想要的是,而不是警报对话框的黑色背景,我想要一个透明的背景.我在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 ();
Run Code Online (Sandbox Code Playgroud)
这是我的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>
Run Code Online (Sandbox Code Playgroud)
这是color.xml
<resources>
<color name="transparent_color">#00000000</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
但它没有帮助我.
我的问题:这可行吗?如果是的话,请你指导我正确的方向吗?
我发现这种方式与AlertDialog.Builder兼容.使用Eclipse中Android"Devices"选项卡中的"转储视图层次结构"按钮,我看到了许多嵌套的FrameLayouts,看起来背景就是这些,与窗口相比.
遍历这些视图并将背景设置为透明的每个实际工作(我的自定义视图然后浮动在昏暗的应用程序上,没有框架或背景,并且布局没有变化).我有一个自定义视图,因此从它遍历,但从窗口遍历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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17103 次 |
| 最近记录: |