【问题标题】:Make alert dialog background transparent使警报对话框背景透明
【发布时间】:2013-06-19 22:28:07
【问题描述】:

我正在 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 ();

这是我在 res/values/styles.xml 中定义的 CustomAlertDialog

<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>

但这对我没有帮助。

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

【问题讨论】:

  • 你的目标只是让它透明吗?或者您选择构建自定义主题还有其他原因吗?即使没有自定义主题也可以带来透明度。
  • 我只需要它是透明的。

标签: android android-layout android-alertdialog android-4.2-jelly-bean


【解决方案1】:

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

【讨论】:

  • 这可行,但是警报对话框的按钮区域没有背景。你是怎么处理的?
【解决方案2】:

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

【讨论】:

  • 谢谢肖比特!!将警报对话框转换为对话框确实帮助我使背景透明。但是,这意味着我需要添加自己的“确定”和“取消”按钮并向它们添加侦听器?另外,现在我看不到对话框的边框了!!
  • 好的。不要转换为Dialog。在您的AlertDialog(不在构建器上)尝试:dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT))
  • 我之前尝试过,但没有成功。我还尝试了 dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0)) ,但也没有用。我不知道为什么给警报对话框提供透明背景很重要。
  • 你是对的。也许这就是他们创建对话框的原因。如果我需要你想要的东西,我通常会使用Dialogs 或“具有对话框主题的活动”。
【解决方案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>

并将活动添加到清单中......

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

这为我完成了这项工作。

希望这会有所帮助。

【讨论】:

  • 这是我读过的非常令人沮丧的答案!作为对话的活动;创造力巅峰。变通办法有限制,不可逾越!
  • @Ahmedna 该文档中明确提到了一个用例。但这可能会使您严格希望在特定Activity 的上下文中保留选项卡的情况变得复杂。准确地说,我的意思是变通办法不是解决方案!但是,是的,作为开发人员,我们必须时不时地修补一些东西。感谢您指出那一位朋友! :)
【解决方案4】:

只需调用,

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

就在之前

customDialog.show();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-02
    • 2013-01-07
    • 2014-03-13
    • 2023-03-29
    • 2017-05-10
    • 2013-03-09
    • 2022-01-23
    • 2012-06-03
    相关资源
    最近更新 更多