使警报对话框背景透明

时间:2013-06-19 22:28:07

标签: 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 ();

这是我的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>

这是color.xml

<resources>
   <color name="transparent_color">#00000000</color>
</resources>

但它对我没有帮助。

我的问题:这可行吗?如果是的话,请你指导我正确的方向吗?

4 个答案:

答案 0 :(得分:7)

我发现这种方式与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;
      }
    }
  }

答案 1 :(得分:3)

如果您还没有阅读Style attributes of custom styled AlertDialog问题,请继续阅读。答案建议使用Dialog代替Alert Dialog。此外,与LayoutInflater阅读Why does LayoutInflater ignore the layout_width and layout_height layout parameters I've specified?问题可能会更清楚一些。希望能帮助到你。尝试一下,让我知道它是否有效。

答案 2 :(得分:3)

我必须为我的一个项目做类似的事情。我所做的是为AlertDialog创建一个活动。

不确定这是否是您所追求的,但在此处发布以防万一...

<强>活动

public class ShowAlertDialogActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert_dialog);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // create the builder

        AlertDialog alert = builder.create();
        alert.show();
    }
}

我在布局中将背景设置为透明( alert_dialog.xml )......

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@android:color/transparent">
</LinearLayout>

并将活动添加到Manifest ......

<activity
    android:name=".ShowAlertDialogActivity"
    android:label="@string/app_title"
    android:theme="@style/AppTheme"
    android:launchMode="singleTask" />

这对我有用。

希望这有帮助。

答案 3 :(得分:3)

只需致电,

customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

之前

customDialog.show();
相关问题