如何改变周围Dia​​logFragment的背景颜色?颜色、背景、Dia、logFragment

2023-09-12 09:09:43 作者:帝霸

我要建一个自定义的 DialogFragment 。对话框的布局设置为 my_dialog.xml ,但我怎么能修改对话框周围的颜色(透明灰色)?

my_dialog.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =中心>

    <的TextView
        机器人:ID =@ + ID /你好
        机器人:layout_width =100dp
        机器人:layout_height =100dp
        机器人:背景=@机器人:彩色/ holo_orange_light
        机器人:重力=中心
        机器人:文本=你好/>

< / RelativeLayout的>
 

MyDialogFragment.java

 公共类MyDialogFragment扩展DialogFragment {

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){

        查看查看= inflater.inflate(R.layout.my_dialog,集装箱);

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

        返回查看;
    }
}
 

解决方案

我必须设置安卓windowIsFloating 设置为false,安卓windowBackground 在对话框的风格我的自定义颜色:

styles.xml

 <资源的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <样式名称=MyDialog父=@安卓风格/ Theme.Dialog>
        <项目名称=机器人:窗框> @空< /项目>
        <项目名称=机器人:windowBackground> @色/ orange_transparent< /项目>
        <项目名称=机器人:windowIsFloating>假< /项目>
        <项目名称=机器人:windowContentOverlay> @空< /项目>
        <项目名称=机器人:windowTitleStyle> @空< /项目>
        <项目名称=机器人:colorBackgroundCacheHint> @空< /项目>
        <项目名称=机器人:windowAnimationStyle> @android:款式/ Animation.Dialog< /项目>
        <项目名称=机器人:windowSoftInputMode> stateUnspecified | adjustPan< /项目>
        <项目名称=安卓重力>中心< /项目>
    < /风格>

< /资源>
 

MyDialogFragment

 公共类MyDialogFragment扩展DialogFragment {

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setStyle(DialogFragment.STYLE_NO_TITLE,R.style.MyDialog);
    }
}
 

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 grey)?

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

解决方案

I had to set android:windowIsFloating to false and android:windowBackground to my custom color in the dialog style:

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

 
精彩推荐
图片推荐