Checkbox is not inflated as AppCompatCheckbox

时间:2016-07-11 20:08:00

标签: android checkbox android-appcompat

I have a simple checkbox in my layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
>

<CheckBox
    android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    />

</RelativeLayout>

My app theme

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/module_color_blue</item>
    <item name="colorPrimaryDark">@color/module_color_purple</item>
    <item name="colorAccent">@color/module_color_green</item>
</style>

My activity Inherits from AppCompatActivity and targets API 23

compile 'com.android.support:appcompat-v7:23.4.0'

Custom View that inflates the checkbox

package com.company.components.calendar.views.filter

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.RelativeLayout
import com.company.app.EventArg
import com.company.app.ResourceManager
import com.company.entities.api.components.calendar.EventType
import cz.company.school.R
import     kotlinx.android.synthetic.main.component_calendar_event_type_view.view.*

class EventTypeView : RelativeLayout {
    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context,     attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) :     super(context, attrs, defStyleAttr) {
        init()
    }

    var OnEnabled = EventArg<Boolean>()

    private fun init() {
        val mInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater;
        val view =     mInflater.inflate(R.layout.component_calendar_event_type_view, this, true);
        view.setOnClickListener {
            checkbox.isChecked = !checkbox.isChecked;
            OnEnabled(checkbox.isChecked)
        }
    }

    lateinit var _type: EventType;
    var type: EventType
        get() = _type
        set(value) {
            _type = value

            checkbox.isChecked = value.enabled
            text_view.text = value.name
            checkbox.supportButtonTintList =     resources.getColorStateList(ResourceManager.getColorId(value.color))
        }
}

The problem

When my layout is inflated the checkbox is inflated as ordinary checkbox, it is not instance of android.support.v7.widget.AppCompatCheckBox. I don't see any errors in the log or anything.

The solution

The problem was that I was passing appContext to the custom component.

2 个答案:

答案 0 :(得分:3)

Appcompat checkbox has got a different name:

<android.support.v7.widget.AppCompatCheckBox
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:theme="@style/checkBoxComponent" />

答案 1 :(得分:2)

When you use context.getSystemService(Context.LAYOUT_INFLATER_SERVICE), you're getting the system default LayoutInfater which doesn't know anything about AppCompat.

You should instead cast your Context to an Activity and call getLayoutInflater() - this ensures you have a reference to the AppCompat LayoutInflater.

val mInflater = ((Activity) context).getLayoutInflater();