简体   繁体   English

在同一应用中同时使用浅色和深色复选框

[英]Use both light and dark checkboxes in same app

I'm using a custom theme whose parent is Light.DarkActionBar 我正在使用自定义主题,其父主题是Light.DarkActionBar

<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">

This produces dark CheckBoxes (text and checkbox drawable) on white backgrounds, as expected and intended. 如预期和预期的那样,这会在白色背景上产生深色的CheckBox(文本和复选框可绘制)。

I can use the dark theme for the inverse: 我可以将深色主题用于反色:

<style name="MyTheme" parent="Theme.AppCompat">

Again, this works as expected (light CheckBox text and graphic). 再次,这按预期方式工作(轻巧的CheckBox文本和图形)。

The app uses the former, for dark checkboxes on light background, but one particular UI (a Fragment) has CheckBoxes on dark backgrounds, so I'd like to flip the colors (to light) for just these specific CheckBoxes. 该应用程序将前者用于浅色背景上的深色复选框,但是一个特定的UI(片段)在深色背景上具有CheckBoxes,所以我想仅将这些特定CheckBox的颜色翻转(变浅)。

I can use textColor to color the label, but the CheckBox outline still appears dark. 我可以使用textColor为标签着色,但是CheckBox轮廓仍然显示为深色。

I can use use tinting in API 21+ (L), but I need to support APIs as low as 16 (using AppCompat). 我可以在API 21+(L)中使用使用着色,但是我需要支持低至16(使用AppCompat)的API。

I've tried setColorFilter as well, but it hasn't worked yet (crashing without any logs - I'm continuing to investigate), but I'm not hopeful about this approach since I'd like the graphic to behave "naturally", for example, switch the accent color during the checked transition. 我也尝试过setColorFilter ,但是它还没有工作(没有任何日志就会崩溃-我正在继续调查),但是我对这种方法不抱希望,因为我希望图形表现得“自然” ,例如,在检查的过渡期间切换强调色。

I don't want to use custom graphics because I want the graphic to be supplied by the system and be familiar to the user, and match other apps on the same system. 我不想使用自定义图形,因为我希望图形由系统提供并为用户所熟悉,并与同一系统上的其他应用程序匹配。

I've tried setting various styles on the CheckBox directly, but without any visual change at all: 我尝试直接在CheckBox上设置各种样式,但根本没有任何视觉更改:

<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
  style="@style/ThemeOverlay.AppCompat.Dark"
  ...

No value for style seems to have any visible impact. 风格的价值似乎没有任何可见的影响。

So how can I get light, system-supplied CheckBox graphics for these specific CheckBoxes, while the other CheckBoxes in the app continue to be dark? 那么,如何使这些特定的CheckBox获得系统提供的浅色CheckBox图形,而应用程序中的其他CheckBox却继续变暗?

TYIA TYIA

View s are themed based on the Context against which they are inflated. View基于膨胀的Context进行主题化。

One way to achieve dark check boxes against a dark background would be to inflate the entire dark section of the view hierarchy against a ContextThemeWrapper created using a dark theme. 在深色背景上实现深色复选框的一种方法是,将使用深色主题创建的ContextThemeWrapper视图层次结构的整个深色部分。

ContextThemeWrapper darkContext = new ContextThemeWrapper(
    context, R.style.MyThemeDark);
LayoutInflater darkInflater = LayoutInflater.from(darkContext);
View myDarkLayout = darkInflater.inflate(
    R.layout.some_layout, parentView, true);

On API 21+, you can achieve this effect using the android:theme attribute. 在API 21+上,您可以使用android:theme属性实现此效果。

some_layout.xml: some_layout.xml:

<LinearLayout
    android:id="@+id/my_dark_layout"
    android:theme="@style/MyDarkTheme"
    ...>
    <CheckBox ... />
</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Android java 应用程序中保持相同的主题? 在 android 手机的暗光模式下 - How to maintain the same theme in Android java app ? in both dark and light mode of android mobile 复选框在明暗模式下呈现不同 - Checkboxes render differently in light and dark mode 如何在Android上的Dark App Bar上使用Light SearchView? - How can I use Light SearchView on Dark App Bar on android? 带有Light App主题的深色工具栏 - Dark Toolbar with Light App Theme 支持Android Pie中的明暗主题 - Support both light and dark theme in Android Pie 切换到浅色模式时应用程序崩溃 - App crashes when changing into light to dark mode 暗/亮模式重新创建应用程序后,SwitchCompats 检查不正确 - SwitchCompats are checked incorrectly after Dark/Light Mode recreates the app 如何使应用程序具有黑暗和明亮的主题? - How can I make an app dark and light theme? 如何使用可组合物在应用程序中动态切换明暗主题 - How to switch between light and dark theme dynamically in app using composables 更改为浅色模式时,React Native App 崩溃 - React native App crashes when changing into light to dark mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM