1
我想我的XML佈局轉換爲安口DSL,但我有與CardView轉換XML到安口DSL
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cardview_manga"
android:layout_margin="5dp">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/manga_cover"
android:background="@color/placeholder_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/cover" />
<TextView
android:id="@+id/manga_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:padding="5dp"
android:background="#3f000000"
android:textColor="#ffffff"
android:textSize="14sp" />
</FrameLayout>
</android.support.v7.widget.CardView>
問題,而這正是安口DSL我轉換爲:
class ItemManga : AnkoComponent<ViewGroup> {
companion object {
lateinit var mangaCover: ImageView
lateinit var mangaName: TextView
}
override fun createView(ui: AnkoContext<ViewGroup>) = ui.apply {
relativeLayout {
cardView {
frameLayout {
lparams {
height = matchParent
width = matchParent
}
mangaCover = imageView {
backgroundColor = R.color.placeholder_background
contentDescription = ctx.getString(R.string.cover)
}.lparams {
height = matchParent
width = matchParent
}
mangaName = textView {
textSize = sp(14).toFloat()
textColor = android.R.color.white
backgroundColor = R.color.manga_title_background
}.lparams {
height = wrapContent
width = matchParent
gravity = Gravity.BOTTOM
padding = dip(5)
}
}
}.lparams {
height = wrapContent
width = matchParent
margin = dip(5)
}
}
}.view
}
結果不是預期的:
- 的CardView佔位符的背景是紫色的,當它應該是灰色的。
- ImageView與CardView不匹配。
- TextView背景是紫色的,它太高,甚至沒有出現。
下面是一些截圖:
你如何使用它?我的意思是我對你在這裏使用伴侶對象有點困惑。 關於顏色,你需要獲得一個Color類的實例,而不是Int - ContextCompat.getColor(context,R.color.your_desired_colour) – Antek
btw,你可以直接寫'override fun createView(ui:AnkoContext)= with (ui){relative} {...}},沒有'.view',以及'textSize = dip(14)' –
Antek