如何设置DialogFragment的主题

时间:2013-06-14 17:11:00

标签: android

有人可以解释为什么这个陈述非常有效:

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 个答案:

答案 0 :(得分:28)

回答可能为时已晚,但尝试使用片段的onCreate中的代码

@Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

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

答案 1 :(得分:1)

尝试设置类似

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

答案 2 :(得分:0)

https://stackoverflow.com/a/24375599/2898715,也可以完全省略setStyle调用,并定义DialogFragments将在整个应用程序中使用的defulat样式。

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>

答案 3 :(得分:0)

从活动中调用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.```

答案 4 :(得分:0)

尝试通过如下编程来实现:-

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

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