私は様々なAPI要求を行うクラスを実装しています、私の考えは、クラスのすべてのインスタンスは、インターフェイスのようなタイルを持つビューを作成するメソッドを持っていたということでした。Ankoビュークラスから
私の問題は、これをどのようにして良い方法で実装するのか分かりません。
AnkoとKotlinを使用してこれを行うのはどのような方法ですか?
私は様々なAPI要求を行うクラスを実装しています、私の考えは、クラスのすべてのインスタンスは、インターフェイスのようなタイルを持つビューを作成するメソッドを持っていたということでした。Ankoビュークラスから
私の問題は、これをどのようにして良い方法で実装するのか分かりません。
AnkoとKotlinを使用してこれを行うのはどのような方法ですか?
アンコはCustomView
カスタムView
クラス名で、 customView
はあなたがDSLで書きたいものです、(しかし、ええ、ドキュメントを読んで誰が?)
さんが言ってみましょう偉大documentation about that caseを持っています。あなただけ に囲まれたDSLでカスタム
View
を使用する予定の場合いくつか
View
その他:frameLayout { customView() }
...またはこの(参照:
inline fun ViewManager.customView(theme: Int = 0) = customView(theme) {} inline fun ViewManager.customView(theme: Int = 0, init: CustomView.() -> Unit) = ankoView({ CustomView(it) }, theme, init)
だから今、あなたはこれを書くことができますUIラッパーの章):
UI { customView() }
しかし、あなたは
Activity
内UI ラッパーなしで、トップレベルのウィジェットとして、あなたのビューを使用したい場合は、同様にこれを追加します。それは私がそれを使用するのと同じ方法です(inline fun Activity.customView(theme: Int = 0) = customView(theme) {} inline fun Activity.customView(theme: Int = 0, init: CustomView.() -> Unit) = ankoView({ CustomView(it) }, theme, init)
例を、あなたは別のアプローチを選択することができ):別のファイルで
class YourAwesomeButton: Button() {
/* ... */
fun makeThisButtonAwesome() {/* ... */}
}
/** This lines may be in any file of the project, but better to put them right under the button class */
inline fun ViewManager.yourAwesomeButton(theme: Int = 0) = yourAwesomeButton(theme) {}
inline fun ViewManager.yourAwesomeButton(theme: Int = 0, init: CustomView.() -> Unit) =
ankoView({ YourAwesomeButton(it) }, theme, init)
:
class YourAwesomeActivity: Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(saveInstanceState)
relativeLayout(R.style.YourAwesomeAppTheme) {
yourAwesomeButton(R.style.YourAwesomeAppTheme) {
makeThisButtonAwesome()
}.lparams {
centerInParent()
}
}
}
}
私はドキュメントのこの部分を読んだが、私には、それは時間にまだ不明ですこのコードスニペットを使用する例がありますか? – aul12
@ aul12に例が追加されました –