Tag: anko

使用Anko访问活动的视图

我知道我可以使用Anko的id属性来识别视图: class MainActivityUI : AnkoComponent<MainActivity> { override fun createView(ui: AnkoContext<MainActivity>) = with(ui) { frameLayout { textView { id = R.id.text } } } } 然后使用find()函数(或使用Kotlin Android扩展)在Activity获取它: class MainActivity : AppCompatActivity() { private val textView by lazy { find<TextView>(R.id.text) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) MainActivityUI().setContentView(this) textView.text = "Hello World" } } 但我觉得我失去了一些东西; README中唯一提到find函数或Kotlin Android扩展的地方在标题为支持现有代码的部分: […]

Anko floatingActionButton缺失或冲突的依赖关系

下面这个稍微过时的教程 ,我卡住试图使用org.jetbrains.anko.design.floatingActionButton 。 错误是: 无法访问类“android.support.design.widget.FloatingActionButton”。 检查您的模块类路径是否存在缺失或冲突的依赖关系。 build.gradle(Project) buildscript { ext.kotlin_version = '1.1.50' ext.anko_version = '0.10.1' repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-beta6' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir } build.gradle(App) apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { […]

使用Kotlin和Anko访问资源ID

嗨,大家好我是Android / Kotlin / Anko的新手,我有一个关于如何访问Anko内部的颜色(也可能是其他)资源的问题。 我知道有一些像textResource这样的textResource ,只需传递R.string.my_color来简化设置资源字符串的过程,但是如何使用View类的Resources实例访问颜色呢? 假设您有Button的子类,并且想要更改文本颜色。 如果你使用textResource它会改变文本字符串而不是颜色,如果你使用textColor那么你必须使用resources.getColor(R.color.my_color, null)来指定真实的资源ID resources.getColor(R.color.my_color, null)这将不会如此讨厌你不必传递可选的主题参数(这里是null ) 在这里创建Resources扩展有用吗? fun Int.fromResources(resources: Resources): Int { return resources.getColor(this, null) } 什么是推荐的方式? 编辑 我改变了textColor值的扩展,做到这一点,我发现最干净的事情,除了我不知道这是否真的是Android友好 var android.widget.TextView.textColor: Int get() = throw AnkoException("'android.widget.TextView.textColor' property does not have a getter") set(v) = setTextColor(resources.getColor(v, null))

为什么这个anko布局不正确地处理系统ui?

我尝试了Anko,不能得到这个布局来处理像xml版本的系统用户界面。 我认为它与主题有关,以及它们是如何通过xml解析来处理的。 这里是截图: 这里是anko DSL组件: class ActivityMainLayout : AnkoComponent<Activity> { lateinit var drawerLayout: DrawerLayout lateinit var appBarLayout: AppBarLayout lateinit var toolbar: Toolbar lateinit var text: TextView lateinit var navHeader: NavigationView lateinit var toggle: ActionBarDrawerToggle override fun createView(ui: AnkoContext<Activity>): View = with(ui) { drawerLayout = drawerLayout { fitsSystemWindows = true coordinatorLayout { appBarLayout = appBarLayout(theme = […]

引起:android.database.sqlite.SQLiteException:近“org”:语法错误(代码1):,编译时:

我正在尝试使用下面的anko库创建表格 override fun onCreate(db: SQLiteDatabase?) { db!!.createTable(PersonTable.Name, true, Pair(PersonTable.ID, INTEGER + PRIMARY_KEY + AUTOINCREMENT), Pair(PersonTable.PersonName, TEXT), Pair(PersonTable.Domain, TEXT), Pair(PersonTable.MobileNumber, REAL)) } 当我将AUTOINCREMENT添加到ID时,出现以下错误。 它没有AUTOINCREMENT 。 使用anko库将任何属性设置为AUTOINCREMENT的方法是什么? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.williamsro.fourthdemo, PID: 4799 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.williamsro.fourthdemo/com.williamsro.fourthdemo.MainActivity}: android.database.sqlite.SQLiteException: near "org": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS `Person`(id […]

在异步调用完成之前按下后退按钮时会发生什么?

我有查看正在存储在应用程序类的数组列表中的模型。 所以即使活动因为旋转而被破坏,它们的生命期也会继续(但是一旦isFinishing为true,那么视图模型实例将从列表中移除并且不再存在)。 我也有数据服务单身人士,视图模型也使用无限期的生活。 所以,如果我开始在我的视图模型使用anko异步块,即异步调用 async { val data = DataService.instance.getData() uiThread { if (data != null) { //do something } } } 如果用户在通话完成之前按下后退按钮,会发生什么情况? IsFinishing将是真实的,我的视图模型实例将不复存在。 请问异步调用异常,当它返回或将它优雅地死亡?

如何将自定义视图组添加到Anko DSL?

Anko文档告诉我们如何将自定义视图添加到DSL。 但是,如果我的自定义视图是视图组,则会出现问题。 class MyFrameLayout(context: Context) : FrameLayout(context) fun ViewManager.myFrameLayout(init: MyFrameLayout.() -> Unit = {}) = ankoView({ MyFrameLayout(it) }, init) class MyUI : AnkoComponent<Fragment> { override fun createView(ui: AnkoContext<Fragment>) = with(ui) { myFrameLayout { textView("hello").lparams { // error: Unresolved reference: lparams bottomMargin = dip(40) } } } } 但如果我改变myFrameLayout调用frameLayout它工作正常。 那么,使视图组与Anko DSL一起使用的正确方法是什么?

状态栏不透明,但是白色

用anko DSL测试kotlin,我决定使用kotlin插件(1.0.3)和最新的anko库(0.9)在最后的android studio ide(2.1.3)中启动一个新的proyect, 我使用默认的导航抽屉活动,所以我只需要将主要的XML转换为ANK。 这是xml: <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.design.widget.AppBarLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" > <TextView android:text="Hello World!" android:layout_width="wrap_content" android:layout_height="wrap_content" /> […]

如何创建像anko – DSL语法Kotlin嵌套函数回调

和anko一样,你可以这样编写回调函数: alert { title = "" message = "" yesButton { toast("Yes") } noButton { toast("No") } } 我怎样才能创建一个这样的嵌套功能? 我试图创建它像下面,但似乎并没有工作。 class Test { fun f1(function: () -> Unit) {} fun f2(function: () -> Unit) {} } 现在,如果我用这个扩展功能, fun Context.temp(function: Test.() -> Unit) { function.onSuccess() // doesn't work } 从Activity调用它: temp { onSuccess { toast("Hello") } […]

如何设置android应用程序的自定义字体?

我似乎无法将标准的android字体更改为我的应用程序中的另一个。 我正在Kotlin写我的应用程序,我正在使用Anko进行布局。 我试过了: typeface = Typeface.create() typeface = Typface.createFromAsset(assets, "font/font_name") setTypeface(Typeface.createFromAsset(assets, "font/font_name")) 谢谢您的帮助。