与同级片段相比,BottomSheetDialog 中的 SwitchCompat 主题颜色错误
SwitchCompat in BottomSheetDialog wrong theme color compared to sibling fragment
我正在努力弄清楚为什么我的 BottomSheetDialog 中的最后一个 SwitchCompat 是绿色而不是黄色(使用布局检查器显示)。
布局检查器说另外两个黄色的 SwitchCompats [在添加到 FrameLayout containerMultiFunctionButtonActions 的片段中] 没有 "style/Theme.Design.Light.BottomSheetDialog()"。
为什么会弄乱我的 SwitchCompat?
正确着色的 SwitchCompats 没有覆盖任何 style/theme,它们与 FrameLayout containerMultiFunctionButtonActions 之间也没有任何元素,所以对我来说,我认为我已经清楚并成功地设置了 style/theme。
这是我的布局:
...
<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>
...
调试这个还需要什么吗?
我真的应该 post 我的 styles/themes 吗?
它们无处不在,但它们似乎在除此 BottomSheetDialog 内部之外的其他任何地方都运行良好。
正如您通过布局检查器发现的那样,不同的主题正在应用于您的开关。
同级片段中的开关仅选取您的应用程序和 activity 的主题,而底部 sheet 中的开关另外选取底部 sheet的主题。
主题的整体思想是它们会影响所有子项的属性,因此在 Theme.Design.Light.BottomSheetDialog
中定义的 SwitchCompat
使用的任何属性都有可能 "change" 当您开关在底部 sheet 内(与不在底部 sheet 内的开关相比)。
如果您查看此主题的来源(并遵循父主题链),您最终会找到这个:
<style name="Base.V7.Theme.AppCompat.Light" parent="Platform.AppCompat.Light">
...
<item name="colorAccent">@color/accent_material_light</item>
...
</style>
不幸的是,colorAccent
定义了开关打开时的颜色,底部 sheet 的主题正在设置它。
您可以通过为 SwitchCompat
更改 colorAccent
的值来解决此问题。您有很多选择 如何 来执行此操作,但最简单的是创建您自己的样式资源并使用 android:theme
属性将其应用于您的开关。像这样:
<style name="YellowColorAccent">
<item name="colorAccent">your yellow color here</item>
</style>
我同意 但符合 BottomSheetDialogFragment
的最简单方法
用你的主题来设置
android:theme="@style/AppTheme"
在您 BottomSheetDialogFragment
的 xml 的根元素中。
我正在努力弄清楚为什么我的 BottomSheetDialog 中的最后一个 SwitchCompat 是绿色而不是黄色(使用布局检查器显示)。
布局检查器说另外两个黄色的 SwitchCompats [在添加到 FrameLayout containerMultiFunctionButtonActions 的片段中] 没有 "style/Theme.Design.Light.BottomSheetDialog()"。
为什么会弄乱我的 SwitchCompat?
正确着色的 SwitchCompats 没有覆盖任何 style/theme,它们与 FrameLayout containerMultiFunctionButtonActions 之间也没有任何元素,所以对我来说,我认为我已经清楚并成功地设置了 style/theme。
这是我的布局:
...
<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>
...
调试这个还需要什么吗?
我真的应该 post 我的 styles/themes 吗?
它们无处不在,但它们似乎在除此 BottomSheetDialog 内部之外的其他任何地方都运行良好。
正如您通过布局检查器发现的那样,不同的主题正在应用于您的开关。
同级片段中的开关仅选取您的应用程序和 activity 的主题,而底部 sheet 中的开关另外选取底部 sheet的主题。
主题的整体思想是它们会影响所有子项的属性,因此在 Theme.Design.Light.BottomSheetDialog
中定义的 SwitchCompat
使用的任何属性都有可能 "change" 当您开关在底部 sheet 内(与不在底部 sheet 内的开关相比)。
如果您查看此主题的来源(并遵循父主题链),您最终会找到这个:
<style name="Base.V7.Theme.AppCompat.Light" parent="Platform.AppCompat.Light">
...
<item name="colorAccent">@color/accent_material_light</item>
...
</style>
不幸的是,colorAccent
定义了开关打开时的颜色,底部 sheet 的主题正在设置它。
您可以通过为 SwitchCompat
更改 colorAccent
的值来解决此问题。您有很多选择 如何 来执行此操作,但最简单的是创建您自己的样式资源并使用 android:theme
属性将其应用于您的开关。像这样:
<style name="YellowColorAccent">
<item name="colorAccent">your yellow color here</item>
</style>
我同意 BottomSheetDialogFragment
用你的主题来设置
android:theme="@style/AppTheme"
在您 BottomSheetDialogFragment
的 xml 的根元素中。