How to change the background color around a DialogFragment?

前端 未结 8 1803
时光取名叫无心
时光取名叫无心 2020-11-29 01:59

I\'m building a custom DialogFragment. The dialog layout is set to my_dialog.xml, but how can I modify the color around the dialog (the transparent

相关标签:
8条回答
  • 2020-11-29 02:37

    To get a fully transparent dialog, you can set in onCreateView the following

    • setBackgroundDrawable to Color.TRANSPARENT
    • setDimAmount to 0

    See code example here:

    public class TextEditor extends DialogFragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_text_editor, container);
    
            // make dialog itself transparent
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    
            // remove background dim 
            getDialog().getWindow().setDimAmount(0);
    
            //[add more custom code here...]
    
            return view;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 02:37

    change your Text Background on the XML i just edited your code replace it with yours

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" >
    
    <TextView
        android:id="@+id/hello"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:gravity="center"
        android:text="hello" />
    

    because you gave the TextView height and width of 100dp. and set a background for it that will fill the whole dialog. since your main layout is wrap_content. please do accept the answer if that helped you.
    Edit : to change the background just add to your layout or to your textview android:background="#232323" you can change these number to any color you like. or you can set a background from the drawable like android:background="@drawable/yourpicturename"

    0 讨论(0)
提交回复
热议问题