3

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>

しかし、それは私を助けませんでした。

私の質問: これは実行可能ですか? はいの場合、正しい方向に私を案内してもらえますか?

4

4 に答える 4

7

AlertDialog.Builder と互換性のあるこの方法を見つけました。Eclipse の Android の [Devices] タブにある [Dump View Hierarchy] ボタンを使用すると、多くのネストされた FrameLayout が表示され、ウィンドウではなく背景がこれらの中にあるように見えました。

これらのビューをトラバースし、それぞれの背景を透明に設定すると、実際に機能しました (私のカスタム ビューは、フレームや背景なしで淡色表示されたアプリの上に浮かび、レイアウトに変更はありません)。私はカスタムビューを持っていたので、そこから下にトラバースしましたが、ウィンドウから上にビューグループをトラバースすることも機能するはずです。

カスタム ビューを持つ独自の 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;
      }
    }
  }
于 2013-11-16T06:42:32.280 に答える
3

カスタム スタイルの AlertDialog質問のスタイル属性をまだ読んでいない場合は、先に進んで読む必要があります。答えは、Dialogの代わりに使用することを提案していますAlert Dialog。さらに読むLayoutInflater が指定した layout_width および layout_height レイアウト パラメータを無視するのはなぜですか? 問題LayoutInflaterはもう少し解決するかもしれません。それが役に立てば幸い。試してみて、うまくいくかどうか教えてください。

于 2013-06-19T22:35:37.440 に答える
3

電話するだけで、

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

直前

customDialog.show();
于 2014-12-01T14:33:11.063 に答える