【问题标题】:How to change the background color around a DialogFragment?如何更改 DialogFragment 周围的背景颜色?
【发布时间】:2013-02-07 01:44:55
【问题描述】:

我正在构建一个自定义 DialogFragment。对话框布局设置为my_dialog.xml,但是如何修改对话框周围的颜色(透明灰色)?

my_dialog.xml

<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:background="@android:color/holo_orange_light"
        android:gravity="center"
        android:text="hello" />

</RelativeLayout>

MyDialogFragment.java

public class MyDialogFragment extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.my_dialog, container);

        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        return view;
    }
}

【问题讨论】:

    标签: android background android-dialogfragment android-styles


    【解决方案1】:

    我必须在对话框样式中将 android:windowIsFloating 设置为 false 并将 android:windowBackground 设置为我的自定义颜色:

    styles.xml

    <resources xmlns:android="http://schemas.android.com/apk/res/android">
    
        <style name="MyDialog" parent="@android:style/Theme.Dialog">
            <item name="android:windowFrame">@null</item>
            <item name="android:windowBackground">@color/orange_transparent</item>
            <item name="android:windowIsFloating">false</item>
            <item name="android:windowContentOverlay">@null</item>
            <item name="android:windowTitleStyle">@null</item>
            <item name="android:colorBackgroundCacheHint">@null</item>
            <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
            <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
            <item name="android:gravity">center</item>
        </style>
    
    </resources>
    

    MyDialogFragment

    public class MyDialogFragment extends DialogFragment {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog);
        }
    }
    

    【讨论】:

    • 成功了,非常感谢。这个带有样式行为的对话框真的很奇怪而且不清楚。
    • 请注意,如果您将windowBackground 更改为一种颜色,您将失去带有圆角的默认DialogFragment 窗口样式。要保留这一点,您必须创建一个以颜色为背景的可绘制矩形(即solid 参数)并使用具有半径值的角。
    • 如何在 Dialog 类中做到这一点?我的意思是我扩展了 Dialog ,而不是 DialogFragment 并且没有 setStyle() ...
    • 搞砸了背景的基本“onClick”内容,但如果您点击背景,对话框将不再关闭
    • 不需要将android:windowFrame设置为@null stackoverflow.com/a/8053718/1621111
    【解决方案2】:

    我想为我的DialogFragment 设置一个透明背景,并且在代码中,这可以正常工作:

    @Override
    public void onStart() {
        super.onStart();
    
        Window window = getDialog().getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
    }
    

    当然,您可以使用setBackgroundDrawable()setBackgroundDrawableResource() 指定任何颜色或Drawable

    这至少适用于 onStart(),但不适用于 onCreate(),不一定适用于 onCreateView()it seems

    也就是说,在大多数情况下,这样做in XML, using styles 可能更干净,遵循以下原则:

    <style name="MyDialogStyle" parent="@android:style/Theme.Holo.Light.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>
    

    【讨论】:

    • 为我工作,伙计们,这就是诀窍,谢谢伙计,节省了我的时间。
    【解决方案3】:

    要获得完全透明的对话框,可以在onCreateView中设置如下

    • setBackgroundDrawableColor.TRANSPARENT
    • setDimAmount0

    在此处查看代码示例:

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

    【讨论】:

    • 这对我来说是应该被接受的最佳答案。
    【解决方案4】:

    我发现我只需要这样做:

    <style name="MyDialog" parent="@android:style/Theme.Dialog">
        <!--  other attributes  -->
        <item name="android:backgroundDimEnabled">false</item>
    </style>
    

    【讨论】:

    • 似乎这应该是公认的答案。接受答案中的 xml 除了删除灰色部分外,还会更改屏幕截图橙色部分的颜色。
    【解决方案5】:

    那些在“onCreateDialog”而不是“onCreateView”中使用 AlertDialog 构建器的人可以像下面的代码一样分配主题。完整的主题集可以从R.style 找到。不要忘记其中一些是最近支持的,并且在旧设备手机上不可用。

    @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
    
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Translucent);
            View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_album, null);
            builder.setView(view);
    
            return builder.create();
        }
    

    【讨论】:

      【解决方案6】:

      覆盖 onCreate 并在那里设置样式应该可以工作。

      @Override
      public void onCreate(Bundle savedInstance){
          super.onCreate(savedInstance);
          setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Translucent);
      }
      

      【讨论】:

        【解决方案7】:

        XML 上更改您的文本Background 我刚刚编辑了你的代码,用你的替换它

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

        因为你给了TextView 100dp 的高度和宽度。并为其设置一个填充整个对话框的背景。因为您的主要布局是 wrap_content。 如果对您有帮助,请务必接受答案。
        编辑:更改background 只需添加到您的布局或文本视图中 android:background="#232323" 您可以将这些数字更改为您喜欢的任何颜色。或者您可以从drawable 设置背景,例如android:background="@drawable/yourpicturename"

        【讨论】:

        • 我已经为我的 TextView 设置了背景。我的问题是 TextView 周围的颜色
        • 颜色是由在你的 textView 中设置背景引起的,因为这个 android:background="@android:color/holo_orange_light" 我已经从你的布局中删除了这个。你能复制并粘贴我上面的代码,看看它是如何工作的!
        • 是的,我知道,我想要这样。我想修改它周围的灰色。
        • 很好地修改灰色。将背景设置为线性布局。或在您的对话框中添加 Android.Theme.Holo 或您喜欢的任何主题,只需在对话框构造函数中定义后添加即可。
        • 我猜你的意思是RelativeLayout。由于它包裹了TextView的内容,改变它的背景不会改变窗口的透明灰色。
        【解决方案8】:

        Jul 的回答对我来说几乎是好事。但是使用 AlertDialog.Builder 时似乎不起作用。

        我必须在这里设置样式:

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
        
            // Use the Builder class for convenient dialog construction
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyDialog );
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-01-25
          • 1970-01-01
          • 2011-11-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-08
          相关资源
          最近更新 更多