如何为DialogFragment设置主题

24

能有人解释一下为什么这个语句可以完美地运行吗:

setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo);

而且下一个声明没有执行

setStyle(DialogFragment.STYLE_NO_TITLE, R.style.dialog);

这是我在风格部门拥有的:

<style
    name="dialog">
    <!-- title encapsulating main part (backgroud) of custom alertdialog -->
    <item
        name="android:windowFrame">@null</item>
        <!-- turn off any drawable used to draw a frame on the window -->
    <item
        name="android:windowBackground">@null</item>
        <!-- turn off any drawable used to draw a frame on the window -->
    <item
        name="android:windowIsFloating">true</item>
        <!-- float the window so it does not fill the screen -->
    <item
        name="android:windowNoTitle">true</item>
        <!-- remove the title bar we make our own-->
    <item
        name="android:windowContentOverlay">@null</item>
        <!-- remove the shadow from under the title bar -->
</style>
5个回答

45

尝试将代码放在片段的onCreate方法中,如下所示:

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

4

https://dev59.com/Pmkv5IYBdhLWcg3wbgfQ#24375599 也可以完全省略 setStyle 调用,并定义整个应用程序将使用的 DialogFragments 默认样式。

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppTheme" parent="android:Theme.Holo.Light">
        <!-- makes all dialogs in your app use your dialog theme by default -->
        <item name="alertDialogTheme">@style/DialogTheme</item>
        <item name="android:alertDialogTheme">?alertDialogTheme</item>
        <item name="dialogTheme">?alertDialogTheme</item>
        <item name="android:dialogTheme">?alertDialogTheme</item>
    </style>

    <!-- define your dialog theme -->
    <style name="DialogTheme" parent="Theme.AppCompat.DayNight.Dialog.MinWidth">
        <item name="android:buttonStyle">@style/button_green</item>
        <item name="android:textColorHint">@color/green</item>
    </style>

</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    .....
    <application android:theme="@style/AppTheme">
        .....
    </application>
</manifest>

1
尝试通过以下编程方式实现:-
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.dialog);

或者也可以在XML文件中声明。

1

尝试设置一个父级主题,如

<style name="dialog" parent="@android:style/Theme.Dialog"> Your theme attributes </style>

@blackbelt:我不能放置图片,所以用文字解释有点困难。 - PageMaker
我尝试过了,但没有变化。直接使用Theme_Holo可以给我期望的深色背景而不带标题,正是我想要的。而使用我自己的样式“dialog”则会呈现出一个我不想要的对话框。 - PageMaker

0

从Activity中调用 BottomSheetDialogFragment:

var bottomFragment = ServiceOtpVerficationFragment()
bottomFragment.setStyle(BottomSheetDialogFragment.STYLE_NO_TITLE,R.style.BottomSheetTheme)
bottomFragment.show(supportFragmentManager,"TAG")

Add this in styles.xml
    
    <style name="BottomSheetTheme" parent="Theme.Design.Light.BottomSheetDialog">        <item name="bottomSheetStyle">@style/BottomSheetStyle</item>
    </style>
    <style name="BottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">        
    <item name="android:background">@android:color/transparent</item>

And In the fragment add extension as `BottomSheetDialogFragment()` to fragment class using colon.```

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接