复选框未膨胀为 AppCompatCheckbox

Checkbox is not inflated as AppCompatCheckbox

我的布局中有一个简单的复选框

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

我的应用主题

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

我的 activity 继承自 AppCompatActivity 和目标 API 23

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

使复选框膨胀的自定义视图

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

问题

当我的布局膨胀时,复选框膨胀为普通复选框,它不是 android.support.v7.widget.AppCompatCheckBox 的实例。我在日志中没有看到任何错误。

解决方案

问题是我将 appContext 传递给自定义组件。

Appcompat 复选框有不同的名称:

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

当您使用 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) 时,您将获得系统默认值 LayoutInfater,它对 AppCompat 一无所知。

您应该将 Context 转换为 Activity 并调用 getLayoutInflater() - 这确保您有对 AppCompat LayoutInflater 的引用。

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