与兄弟片段相比,BottomSheetDialog中的SwitchCompat错误的主题颜色

swo*_*oby 2 android android-layout bottom-sheet

绿色的SwitchCompat

我正在试着弄清楚为什么我的BottomSheetDialog中的最后一个SwitchCompat是绿色而不是黄色(使用布局检查器显示).

布局检查器说,另外两个黄色[在添加到FrameLayout containerMultiFunctionButtonActions的片段中]的SwitchCompats没有"style/Theme.Design.Light.BottomSheetDialog()".

黄色的SwitchCompat

为什么会弄乱我的SwitchCompat?

正确颜色的SwitchCompats不会覆盖任何样式/主题,它们之间也没有任何元素和FrameLayout containerMultiFunctionButtonActions,所以对我来说,我认为我已经清楚并成功地设置了样式/主题.

这是我的布局:

...

<TableRow>

    <LinearLayout
        android:id="@+id/groupMultiFunctionButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginTop="4dp"
        android:layout_span="2"
        android:orientation="vertical"
        android:visibility="visible"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            >

            <TextView
                android:id="@+id/textMultiFunctionButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/multi_function_button"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/app_primary_text"
                />

        </LinearLayout>

        <FrameLayout
            android:id="@+id/containerMultiFunctionButtonActions"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="4dp"
            android:layout_marginStart="4dp"
            android:background="@color/pb_gray_light"
            />

    </LinearLayout>

</TableRow>

<TableRow>

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/switchLost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_span="2"
        android:text="Why am I green?!?!?!"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/pb_action_item_text"
        />
        <!--
        NOTE: textAppearance and textColor understandably have no effect on the switch color
        -->

</TableRow>

...
Run Code Online (Sandbox Code Playgroud)

还需要调试这个吗?
我应该真的发布我的风格/主题吗?
他们到处都是,但除了这个BottomSheetDialog之外,它们似乎在其他地方工作正常.

Saf*_*eer 7

我同意答案,但最简单的方法来符合BottomSheetDialogFragment

你的主题是设置

android:theme="@style/AppTheme"
Run Code Online (Sandbox Code Playgroud)

在你BottomSheetDialogFragment的xml 的根元素中.


Ben*_* P. 4

正如您通过布局检查器发现的那样,不同的主题被应用于您的开关。

同级片段中的开关仅拾取应用程序和活动的主题,而底部工作表中的开关还拾取底部工作表的主题。

主题的整体思想是它们影响所有子项的属性,因此当您的开关位于底部工作表内时(与不在底部工作表内的开关相比) SwitchCompat,主题中定义的任何属性Theme.Design.Light.BottomSheetDialog都有可能“更改”。

如果您查看该主题的源代码(并遵循父主题链),您最终会发现:

<style name="Base.V7.Theme.AppCompat.Light" parent="Platform.AppCompat.Light">
    ...
    <item name="colorAccent">@color/accent_material_light</item>
    ...
</style>
Run Code Online (Sandbox Code Playgroud)

对您来说不幸的colorAccent是,定义了开关打开时的颜色,而底部工作表的主题正在设置它。

colorAccent您可以通过更改的值来解决此问题SwitchCompat对于如何执行此操作,您有很多选择,但最简单的方法是创建您自己的样式资源并使用属性将其应用到您的开关android:theme。像这样的东西:

<style name="YellowColorAccent">
    <item name="colorAccent">your yellow color here</item>
</style>
Run Code Online (Sandbox Code Playgroud)