如何在BottomSheetDialog中更改字体和强调颜色?

niwlg2el  于 2022-10-09  发布在  Android
关注(0)|答案(2)|浏览(218)

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

但当我运行这款应用程序时,颜色和字体就变成了默认设置。以下是输出:

请注意,在Android Studio的设计编辑器中,所有的字体和颜色看起来都和预期的一样,但在真正的设备上它们会发生变化。我还使用AppCompat[默认]作为我的活动‘s.xml中的主题

下面是我的XML代码、样式.xml和activity.java
Styles.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>

Layout.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 thatn 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>

Activity.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();
lf5gs5x2

lf5gs5x21#

如果您的类正在扩展BottomSheetDialogFragment。

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

    }
yruzcnhs

yruzcnhs2#

这就是我的类似场景的工作方式

<!-- Base application theme. -->
    <style name="Theme.ExapleApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
          <item name="bottomSheetDialogTheme">@style/DialogStyle</item>
    </style>

并将accent颜色设置为bottom sheet

<style name="DialogStyle" parent="Theme.MaterialComponents.BottomSheetDialog">
    <item name="colorAccent">@color/accent</item>
</style>

这会将accent颜色应用于所有对话框

祝你好运

相关问题