2

我正在使用带有主题的方法来添加拐角边缘,但底部工作表会使应用程序崩溃。这是 BottomSheet 本身

class MyBottomSheet : BottomSheetDialogFragment() {
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NORMAL, R.style.ThemeOverlay_App_BottomSheetDialog)
    }

这里是风格

    <style name="ThemeOverlay.App.BottomSheetDialog" parent="ThemeOverlay.MaterialComponents.Dialog">
        <item name="bottomSheetStyle">@style/CustomBottomSheet</item>
    </style>

    <style name="CustomBottomSheet" parent="Widget.MaterialComponents.BottomSheet">
        <item name="shapeAppearanceOverlay">@style/CustomShapeAppearanceOverlay.MaterialComponents.BottomSheet</item>
    </style>

    <style name="CustomShapeAppearanceOverlay.MaterialComponents.BottomSheet" parent="">
        <item name="cornerSizeTopRight">16dp</item>
        <item name="cornerSizeTopLeft">16dp</item>
        <item name="cornerSizeBottomRight">0dp</item>
        <item name="cornerSizeBottomLeft">0dp</item>
    </style>

Widget.MaterialComponents.BottomSheet当我添加任何将该主题扩展到应用程序的样式时,我遇到了该部分的问题并<item name="bottomSheetStyle">@style/CustomBottomSheet</item>出现错误日志:

android.view.InflateException: Binary XML file line #41: Could not inflate Behavior subclass com.google.android.material.bottomsheet.BottomSheetBehavior
    Caused by: java.lang.RuntimeException: Could not inflate Behavior subclass com.google.android.material.bottomsheet.BottomSheetBehavior
        at androidx.coordinatorlayout.widget.CoordinatorLayout.parseBehavior(CoordinatorLayout.java:649)
        at androidx.coordinatorlayout.widget.CoordinatorLayout$LayoutParams.<init>(CoordinatorLayout.java:2896)
        at androidx.coordinatorlayout.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:1740)
        at androidx.coordinatorlayout.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:112)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.view.View.inflate(View.java:24354)
        at com.google.android.material.bottomsheet.BottomSheetDialog.ensureContainerAndBehavior(BottomSheetDialog.java:188)
        at com.google.android.material.bottomsheet.BottomSheetDialog.wrapInBottomSheet(BottomSheetDialog.java:200)
        at com.google.android.material.bottomsheet.BottomSheetDialog.setContentView(BottomSheetDialog.java:93)
        at androidx.fragment.app.DialogFragment$4.onChanged(DialogFragment.java:155)
        at androidx.fragment.app.DialogFragment$4.onChanged(DialogFragment.java:140)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:133)
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:151)
        at androidx.lifecycle.LiveData.setValue(LiveData.java:309)
        at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2961)
        at androidx.fragment.app.DialogFragment.performCreateView(DialogFragment.java:486)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:515)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2177)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2088)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1990)
        at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at androidx.coordinatorlayout.widget.CoordinatorLayout.parseBehavior(CoordinatorLayout.java:647)
        at androidx.coordinatorlayout.widget.CoordinatorLayout$LayoutParams.<init>(CoordinatorLayout.java:2896) 
        at androidx.coordinatorlayout.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:1740) 
        at androidx.coordinatorlayout.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:112) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:865) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:866) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
        at android.view.View.inflate(View.java:24354) 
        at com.google.android.material.bottomsheet.BottomSheetDialog.ensureContainerAndBehavior(BottomSheetDialog.java:188) 
        at com.google.android.material.bottomsheet.BottomSheetDialog.wrapInBottomSheet(BottomSheetDialog.java:200) 
        at com.google.android.material.bottomsheet.BottomSheetDialog.setContentView(BottomSheetDialog.java:93) 
        at androidx.fragment.app.DialogFragment$4.onChanged(DialogFragment.java:155) 
        at androidx.fragment.app.DialogFragment$4.onChanged(DialogFragment.java:140) 
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:133) 
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:151) 
        at androidx.lifecycle.LiveData.setValue(LiveData.java:309) 
        at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50) 
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2961) 
        at androidx.fragment.app.DialogFragment.performCreateView(DialogFragment.java:486) 
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:515) 
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282) 
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2177) 
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2088) 
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1990) 
        at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
     Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f0400c1 a=2}
        at android.content.res.TypedArray.getColorStateList(TypedArray.java:546)
        at com.google.android.material.resources.MaterialResources.getColorStateList(MaterialResources.java:71)
        at com.google.android.material.bottomsheet.BottomSheetBehavior.<init>(BottomSheetBehavior.java:281)
        at java.lang.reflect.Constructor.newInstance0(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
        at androidx.coordinatorlayout.widget.CoordinatorLayout.parseBehavior(CoordinatorLayout.java:647) 
        at androidx.coordinatorlayout.widget.CoordinatorLayout$LayoutParams.<init>(CoordinatorLayout.java:2896) 
        at androidx.coordinatorlayout.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:1740) 
        at androidx.coordinatorlayout.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:112) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:865) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:866) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
        at android.view.View.inflate(View.java:24354) 
        at com.google.android.material.bottomsheet.BottomSheetDialog.ensureContainerAndBehavior(BottomSheetDialog.java:188) 
        at com.google.android.material.bottomsheet.BottomSheetDialog.wrapInBottomSheet(BottomSheetDialog.java:200) 
        at com.google.android.material.bottomsheet.BottomSheetDialog.setContentView(BottomSheetDialog.java:93) 
        at androidx.fragment.app.DialogFragment$4.onChanged(DialogFragment.java:155) 
        at androidx.fragment.app.DialogFragment$4.onChanged(DialogFragment.java:140) 
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:133) 
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:151) 
        at androidx.lifecycle.LiveData.setValue(LiveData.java:309) 
        at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50) 
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2961) 
        at androidx.fragment.app.DialogFragment.performCreateView(DialogFragment.java:486) 
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:515) 
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282) 
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2177) 
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2088) 
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1990) 
        at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

我试图像这样覆盖背景色调

    <style name="CustomBottomSheet" parent="Widget.MaterialComponents.BottomSheet">
        <item name="backgroundTint">@color/white</item>
    </style>

然后它起作用了!但底部工作表在左侧和右侧有巨大的填充(即不是从屏幕的边缘到边缘)。正如我所见,默认值存在一些问题?attr/colorSurface,当我设置<item name="backgroundTint">?attr/colorSurface</item>应用程序崩溃时,为什么会发生这种情况?

4

1 回答 1

2

崩溃是因为缺少?attr/colorSurface主题,必须将其添加到使用的主应用程序主题中。

    <style name="Common.MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/primary_material</item>
        <item name="colorPrimaryDark">@color/primary_material</item>
        <item name="colorAccent">@color/primary_accent</item>
        <item name="colorSurface">@color/white</item> <-- added it explicitly
        <item name="bottomSheetDialogTheme">@style/Common.BottomSheet.Rounded</item>
    </style>
于 2021-03-05T07:44:53.773 回答