我收到的错误是我的XML预览器中缺少类。所以我无法在预览中看到我的布局,但是当我运行应用程序时,我可以看到一切都很好。
这些类位于我的android studio项目中的一个模块内,并且包括在内:
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 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)
}
}
}
}
我似乎无法确定为什么它没有出现在预览器中。有没有人知道为什么没有出现?
答案 0 :(得分:0)
它是Android工作室Canary 4的一个错误你需要使用Canary 1直到他们修复它! :)