如何在扩展BottomSheetDialogFragment的片段中设置主题?

10
我有一个应用程序,其中包含一个MainActivity,其中包含一个扩展了BottomSheetDialogFragment的片段。 我想设置片段主题,但它仍然保持不变。 请帮忙。
扩展BottomSheetDialogFragment的片段代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:theme="@style/CoffeeDialog">

<de.hdodenhof.circleimageview.CircleImageView
    android:id="@+id/img_Camera"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_gravity="center_horizontal|bottom"
    android:layout_marginTop="10dp"
    android:src="@drawable/amazon"
    app:civ_border_color="@color/textColor"
    app:civ_border_width="5dp"
    app:civ_fill_color="@color/colorPrimary" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/notification_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="8dp" />

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="30dp">

</FrameLayout>

我在使用android:theme="@style/CoffeeDialog",但它没有起作用。
主题代码:
  <style name="CoffeeDialog" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="android:windowCloseOnTouchOutside">false</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:colorBackground">@android:color/transparent</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:backgroundDimAmount">0.3</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
</style>

注意:我只想改变片段主题,而不是 MainActivity。
2个回答

30

尝试以下给出的代码

 public class CustomDialogFragment extends BottomSheetDialogFragment {

      @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NORMAL, R.style.CoffeeDialog);
      }

      ...
    }

CoffeeDialog 将作为主题添加到您的 styles.xml 中。


4
可以,这个功能运作得很好。不过有没有一种方法可以将其包含在应用程序主题中? - Mr.Noob

7
在你的片段中使用getTheme方法添加你的主题,代码如下:
override fun getTheme(): Int {
        return R.style.CustomBottomSheetDialog
    }

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