android Studio 3.0 Canary 4 ui 预览中缺少 类

Missing classes in android studio 3.0 canary 4 ui preview

我收到错误消息,我的 XML 预览器中缺少 classes。所以我无法在预览中看到我的布局,但是当我 运行 应用程序时,我可以看到一切都很好。

classes 在我的 android 工作室项目中的一个模块中,包含如下:

dependencies {
    compile project(":styleguide")
}

并在 XML 中这样使用:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="64dp"
    android:orientation="vertical">

        <com.company.packagename.buttons.SecondaryButtonLink
            android:layout_height="wrap_content"
            android:layout_width="match_parent"/>

</LinearLayout>

按钮看起来像这样:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/base_secondary_button_link_ll"
          android:layout_width="match_parent"
          android:layout_height="@dimen/button_height"
          android:clickable="true"
          style="@style/SelectableItemBackground"
          android:orientation="horizontal">

    <com.company.packagename.textviews.LabelTextView
        android:id="@+id/secondary_button_link_title_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textColor="@drawable/selector_secondary_button_link"
        android:drawableRight="@drawable/arrow_light_blue"
        android:drawablePadding="@dimen/button_icon_left_margin"/>

</merge>

按钮class后面的代码是这样的:

class SecondaryButtonLink @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    init {
        initializeView(attrs)
    }

    private fun initializeView(attrs: AttributeSet?) {
        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        inflater.inflate(R.layout.secondary_button_link, this)

        if (attrs == null) return
        checkForImage(attrs)
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.StyleGuideButton)
        if (typedArray.hasValue(R.styleable.StyleGuideButton_title)) {
            setButtonTitle(typedArray)
        }

    }

    private fun setButtonTitle(typedArray: TypedArray) {
        for (i in 0..typedArray.indexCount - 1) {
            val attr = typedArray.getIndex(i)
            if (attr == R.styleable.StyleGuideButton_title) {
                secondary_button_link_title_tv.text = typedArray.getString(attr)
            }
        }
    }

    private fun checkForImage(attrs: AttributeSet){
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.SecondaryButtonLink)
        if (typedArray.hasValue(R.styleable.SecondaryButtonLink_rightimage)) {
            setImage(typedArray)
        }
    }

    private fun setImage(typedArray: TypedArray){
        for (i in 0..typedArray.indexCount - 1) {
            val attr = typedArray.getIndex(i)
            if (attr == R.styleable.SecondaryButtonLink_rightimage) {
                secondary_button_link_title_tv.setCompoundDrawablesWithIntrinsicBounds(null, null, typedArray.getDrawable(attr),null)
            }
        }
    }
}

我似乎无法确定为什么它没有出现在预览器中。有谁知道为什么没有出现?

它是 android studio Canary 4 上的一个错误,您只需使用 Canary 1 直到他们修复它! :)