BottomSheetDialogFragment更改定义的背景色

时间:2018-10-16 08:32:54

标签: android bottom-sheet

我将BottomSheetDialogfragment背景色设置为与我的bottomAppBar相同的原色#344955

我的BottomAppBar

My BottomAppBar

但是bottomsheetfragment将该颜色更改为其他颜色值,例如bottomsheetfargment

我的BottomSheet

My BottomSheet

我查看了this post,但没有找到解决问题的方法。 我的BottomSheetDialogFragment xml是

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:behavior_hideable="true"
android:background="@color/colorPrimary"
app:layout_behavior="@string/bottom_sheet_behavior"
tools:context=".ui.fragments.BottomSheetFragment">
<androidx.appcompat.widget.AppCompatImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    android:padding="16dp"
    android:src="@drawable/ic_close_24dp"/>
<com.google.android.material.navigation.NavigationView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:menu="@menu/bottom_nav_drawer_menu">

</com.google.android.material.navigation.NavigationView>

我的片段代码是

class BottomSheetFragment : BottomSheetDialogFragment() {

    companion object {
        fun newInstance() = BottomSheetFragment()
    }

    private lateinit var viewModel: BottomSheetViewModel

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.bottom_sheet_fragment, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProviders.of(this).get(BottomSheetViewModel::class.java)
        // TODO: Use the ViewModel
    }

}

我的样式xml是

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorSecondary">#FFFFFF</item>
        <item name="bottomSheetDialogTheme">@style/bs</item>
    </style>

    <style name="bs" parent="Theme.MaterialComponents.BottomSheetDialog">
    </style>

</resources>

1 个答案:

答案 0 :(得分:0)

尝试这种方式

<style name="Base.V7.Theme.AppCompat.Light" parent="Platform.AppCompat.Light">

        <item name="colorAccent">@color/accent_material_light</item>

    </style>