首页 > 解决方案 > 如何更改 BottomSheetDialog 中的字体和强调色?

问题描述

我正在使用 BottomSheet Dialog 显示一个简单的对话框,在构建编辑器中如下所示:

在此处输入图像描述


但是当我运行应用程序时,颜色和字体会变成默认值。下面是输出:

在此处输入图像描述

请注意,所有字体和颜色在 android studio 的设计编辑器中看起来都符合预期,但它们在真实设备上会发生变化。我也在我的活动's.xml 中使用 AppCompat [默认] 作为主题

这是我的 xml 代码和 styles.xml 和 activity.java

样式.xml

    <style name="BottomSheetDialogTheme" parent="Theme.Design.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/BottomSheetStyle</item>
    </style>

    <style name="BottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
        <item name="android:background">@android:color/transparent</item>
        <item name="fontFamily">@font/lato</item>

布局.xml

<RelativeLayout 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="wrap_content"
    android:background="@drawable/bottom_sheet_back"
    android:paddingStart="20dp"
    android:paddingTop="24dp"
    android:paddingEnd="20dp"
    android:paddingBottom="16dp"
    app:layout_behavior="@string/bottom_sheet_behavior">

    <TextView
        android:id="@+id/txtBottomDonate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/lato_bold"
        android:text="Please select the option that\n applies to you"
        android:textColor="#2B2E33"
        android:textSize="20sp" />

    <ImageView
        android:id="@+id/imgBottomDonate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="3dp"
        android:src="@drawable/ic_neft" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtBottomDonate"
        android:layout_marginTop="32dp"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/radioIndian"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:fontFamily="@font/lato"
            android:text="Indian Citizen"
            android:textColor="#2B2E33"
            android:textSize="16sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0.3dp"
            android:layout_marginTop="16dp"
            android:background="#D9D9D9"
            android:layout_marginStart="35dp"
            android:layout_marginBottom="14dp" />

        <RadioButton
            android:id="@+id/radioForeign"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/lato"
            android:text="Foreign/Dual Passport Holder"
            android:textColor="#2B2E33"
            android:textSize="16sp" />
    </RadioGroup>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/radioGroup"
        android:paddingTop="30dp"
        android:layout_marginTop="7dp"
        android:paddingBottom="16dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnCancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Cancel"
            android:textColor="#2196F3"
            android:textAllCaps="false"
            android:layout_weight="1"
            android:fontFamily="@font/lato_bold"
            android:textSize="16sp"
            android:background="@drawable/cancel_btn_back"
            android:layout_marginEnd="5dp"/>

        <Button
            android:id="@+id/btnNext"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Next"
            android:textColor="#FFFFFF"
            android:background="@drawable/next_btn_back"
            android:layout_weight="1"
            android:fontFamily="@font/lato_bold"
            android:textSize="16sp"
            android:textAllCaps="false"
            android:layout_marginStart="5dp"/>
    </LinearLayout>


</RelativeLayout>

活动.java

case (R.id.btnDonate):
                final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(
                        DonationActivity.this, R.style.BottomSheetDialogTheme
                );
                View bottomSheetView = LayoutInflater.from(getApplicationContext())
                        .inflate(
                                R.layout.bottom_sheet_neft,
                                (RelativeLayout)findViewById(R.id.bottomSheet)
                        );
                bottomSheetView.findViewById(R.id.btnCancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomSheetDialog.dismiss();
                    }
                });
                bottomSheetDialog.setContentView(bottomSheetView);
                bottomSheetDialog.show();

标签: androidlayoutbottom-sheet

解决方案


如果您的课程正在扩展 BottomSheetDialogFragment。

 @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new BottomSheetDialog(Objects.requireNonNull(getActivity()), R.style.BottomSheetDialogTheme);

    }

推荐阅读