如何使对话片段的背景变为不可见?

huangapple go评论109阅读模式
英文:

How do I make a Dialog Fragment's background invisible?

问题

I used youtuber Coding in Flow's method to create a custom dialog. I've been trying all day to make the dialog's background transparent. I've used every single method I've found online. Non worked.

Here's how it goes:

  • First here's the Dialog layout layout_dialog.xml :
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:background="@drawable/dialog_background">

        <!-- The contents of the Dialog go here -->

    </RelativeLayout>

    <ImageView
        android:id="@+id/iconImageView2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="0dp"
        android:background="?attr/actionBarItemBackground"
        android:scaleType="fitCenter"
        app:srcCompat="@android:mipmap/sym_def_app_icon" />

</RelativeLayout>
  • Here's the Dialog class:
public class DialogBrightness extends AppCompatDialogFragment {

    // declare whatever variables here

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.layout_dialog, null);

        builder.setView(view)
            .setTitle("Login")
            .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                }
            })
            .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    // get whatever values
                    listener.apply(/* values */);
                }
            });

        // findViewById for your dialog contents here

        return builder.create();
    }

    public interface DialogBrightnessListener {
        void apply(/* values */);
    }
}
  • And here's the Dialog being called from the Main activity:
DialogBrightness dialogBrightness = new DialogBrightness();
dialogBrightness.show(getSupportFragmentManager(), "Brightness Dialog");

This is how the Dialog appears:

如何使对话片段的背景变为不可见?

I'm trying to make the top white part invisible. Nothing works!

英文:

I used youtuber Coding in Flow's method to create a custom dialog. I've been trying all day to make the dialog's background transparent. I've used every single method I've found online. Non worked.

Here's how it goes:

  • First here's the Dialog layout layout_dialog.xml : <?xml version="1.0" encoding="utf-8"?>

    &lt;RelativeLayout
        xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
            xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot; &gt;
    
        &lt;RelativeLayout
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginTop=&quot;50dp&quot;
            android:background=&quot;@drawable/dialog_background&quot;&gt;
    
            &lt;!-- The contents of the Dialog go here --&gt;
    
        &lt;/RelativeLayout&gt;
    
        &lt;ImageView
            android:id=&quot;@+id/iconImageView2&quot;
            android:layout_width=&quot;100dp&quot;
            android:layout_height=&quot;100dp&quot;
            android:layout_alignParentTop=&quot;true&quot;
            android:layout_centerHorizontal=&quot;true&quot;
            android:layout_marginTop=&quot;0dp&quot;
            android:background=&quot;?attr/actionBarItemBackground&quot;
            android:scaleType=&quot;fitCenter&quot;
            app:srcCompat=&quot;@android:mipmap/sym_def_app_icon&quot; /&gt;
    
    &lt;/RelativeLayout&gt;
    
  • Here's the Dialog class:

     public class DialogBrightness extends AppCompatDialogFragment {
    
         //declare whatever variables here
    
         @Override
         public Dialog onCreateDialog(Bundle savedInstanceState) {
    
             AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    
             LayoutInflater inflater = getActivity().getLayoutInflater();
             View view = inflater.inflate(R.layout.layout_dialog, null);
    
             builder.setView(view)
                     .setTitle(&quot;Login&quot;)
                     .setNegativeButton(&quot;cancel&quot;, new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialogInterface, int i) {
                         }
                     })
                     .setPositiveButton(&quot;ok&quot;, new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialogInterface, int i) {
                             //get whatever values
                             listener.apply(//values);
                         }
                     });
    
             //findViewById for your dialog contents here
    
             return builder.create();
         }
    
         public interface DialogBrightnessListener {
             void apply(//values);
         }
     }
    
  • And here's the Dialog being called from the Main activity:

    DialogBrightness dialogBrightness = new DialogBrightness();
    dialogBrightness.show(getSupportFragmentManager(), &quot;Brightness Dialog&quot;);
    

This is how the Dialog appears:

如何使对话片段的背景变为不可见?

I'm trying to make the top white part invisible. Nothing works!

答案1

得分: 4

尝试这个:

将以下代码放在onCreateDialog中:

// 将对话框背景设置为透明
getDialog().getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));

// 移除背景模糊
getDialog().getWindow().setDimAmount(0);

英文:

Try this:

put the code below in the onCreateDialog:

    // set the dialog background to transparent
    getDialog().getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));

    // remove background dim 
    getDialog().getWindow().setDimAmount(0);

答案2

得分: 2

你可以设计布局如下。有一个额外的布局,但在对话框的情况下,它会有所帮助。

如何使对话片段的背景变为不可见?

英文:

You can design the layout like following. There is an extra layout, but in case of dialogs, it will help

如何使对话片段的背景变为不可见?

答案3

得分: 0

将父布局的背景颜色设置为:

android:background="@android:color/transparent"

像这样:


英文:

Try this:

set the background color of the parent layout to:

android:background=&quot;@android:color/transparent&quot;

like this:

&lt;RelativeLayout
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:background=&quot;@android:color/transparent&quot; &gt;

&lt;RelativeLayout
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_marginTop=&quot;50dp&quot;
    android:background=&quot;@drawable/dialog_background&quot;&gt;

    &lt;!-- The contents of the Dialog go here --&gt;

&lt;/RelativeLayout&gt;

&lt;ImageView
    android:id=&quot;@+id/iconImageView2&quot;
    android:layout_width=&quot;100dp&quot;
    android:layout_height=&quot;100dp&quot;
    android:layout_alignParentTop=&quot;true&quot;
    android:layout_centerHorizontal=&quot;true&quot;
    android:layout_marginTop=&quot;0dp&quot;
    android:background=&quot;?attr/actionBarItemBackground&quot;
    android:scaleType=&quot;fitCenter&quot;
    app:srcCompat=&quot;@android:mipmap/sym_def_app_icon&quot; /&gt;

&lt;/RelativeLayout&gt;

huangapple
  • 本文由 发表于 2020年8月1日 20:18:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63205115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定