简体   繁体   中英

Use both light and dark checkboxes in same app

I'm using a custom theme whose parent is 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.

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).

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.

I can use textColor to color the label, but the CheckBox outline still appears dark.

I can use use tinting in API 21+ (L), but I need to support APIs as low as 16 (using AppCompat).

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.

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:

<?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?

TYIA

View s are themed based on the Context against which they are inflated.

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 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.

some_layout.xml:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM