【问题标题】:How to set the theme for DialogFragment如何为 DialogFragment 设置主题
【发布时间】:2013-06-11 10:02:00
【问题描述】:

有人能解释一下为什么这个说法非常有效吗:

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>

【问题讨论】:

    标签: android


    【解决方案1】:

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

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

    也可以在xml文件中声明。

    【讨论】:

      【解决方案2】:

      尝试使用片段的onCreate中的代码作为

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

      【讨论】:

        【解决方案3】:

        从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.```
        

        【讨论】:

          【解决方案4】:

          https://stackoverflow.com/a/24375599/2898715 也可以完全省略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>
          

          【讨论】:

            【解决方案5】:

            尝试设置父主题,例如

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

            【讨论】:

            • @blackbelt:我不能放图片,所以很难用语言解释。
            • 我试过了;但没有变化。直接使用 Theme_Holo 给了我预期的没有标题的深色背景;正是我想要的。使用我自己的风格“对话框”给我一个我不想要的对话框。
            猜你喜欢
            • 2011-07-08
            • 2013-05-30
            • 2019-04-16
            • 1970-01-01
            • 1970-01-01
            • 2016-04-15
            • 1970-01-01
            • 1970-01-01
            • 2015-07-20
            相关资源
            最近更新 更多