美文网首页
android中的一些小tip(一)

android中的一些小tip(一)

作者: Hflydragon | 来源:发表于2020-06-08 12:05 被阅读0次

    最近把kotlin学习了一遍,参照谷歌的安卓官网教程进行自学安卓,在此记录学习过程中遇到的一些问题,以及一些好用的东西。
    本人从事iOS开发已经四年多了,断断续续也学习了前端的一些框架,React、Flutter、Taro。将会把安卓开发过程中的一些知识点汇集在这里,共勉,共同进步。

    安卓中的布局

    布局在以 xml 后缀文件中,可以在 Design 的模式下,通过拖动 Palette 面板中的控件来快速创建一些界面。也可以切换到 code 模式下,手写布局文件进行布局。

    安卓中的样式

    安卓中的样式可以在 Design 的模式下,使用右侧面板进行 Attributes 和一些基础设置,可以将设置 padding 、margin 、字体大小 等,可以通过抽取


    抽取样式

    可以直接导出样式


    导出样式
    管理字符串
    统一管理字符串

    安卓中的图片兼容

    展开res文件夹,然后展开drawable,默认情况下这些文件为矢量可绘制对象。矢量可绘制对象相对于位图图像格式(如PNG)的好处是,矢量可绘制对象可以缩放而不损失质量。同样,矢量可绘制对象通常是一个比位图格式的相同图像小的文件。

    关于矢量可绘制对象的重要注意事项是API 21及更高版本支持它们。但是,应用程序的最低SDK设置为API19。如果您在API 19设备或仿真器上尝试过该应用程序,则会发现该应用程序似乎可以正常构建和运行。那么这是如何工作的呢?

    一、打开 build.gradle(Module:app)。将此行添加到以下defaultConfig部分:

    vectorDrawables.useSupportLibrary = true
    

    二、点击右上角的 Sync Now
    三、打开 .xml 文件,在根控件中输入

    xmlns:app="http://schemas.android.com/apk/res-auto"
    

    四、在使用<ImageVIew>的部分将 android:src 修改为
    app:srcCompat="@drawable/empty_dice"

    生成并运行应用程序。不会在屏幕上看到任何不同,但是现在应用程序无论在何处运行都无需使用生成的PNG文件来生成图像,这意味着应用程序文件更小。


    这一段摘自官方 Kotlin

    使用Timber打印一些信息

    要使用Timber,需要引入依赖,在dependencies中中添加以下依赖
    implementation 'com.jakewharton.timber:timber:4.7.1'
    具体版本号可依据github上最新的版本修改以下。
    使用如下:

    Timber.i("onDestroy Called")
    等效于
    Log.i("你的activity","onDestroy Called")
    

    使用Lifecycle进行生命周期管理

    一些场景需要用到如计数器等需要生命周期管理的对象时,在配套的 onStart 和 onStop 都要写一些胶水代码,如果需要管理很多类似的代码时,则非常容易出错。此时Lifecycle将非常便捷

    如下:

    class DessertTimer(lifecycle: Lifecycle) : LifecycleObserver {
    
        // The number of seconds counted since the timer started
        var secondsCount = 0
    
        /**
         * [Handler] is a class meant to process a queue of messages (known as [android.os.Message]s)
         * or actions (known as [Runnable]s)
         */
        private var handler = Handler()
        private lateinit var runnable: Runnable
    
        init {
    
            lifecycle.addObserver(this)
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        fun startTimer() {
            // Create the runnable action, which prints out a log and increments the seconds counter
            runnable = Runnable {
                secondsCount++
                Timber.i("Timer is at : $secondsCount")
                // postDelayed re-adds the action to the queue of actions the Handler is cycling
                // through. The delayMillis param tells the handler to run the runnable in
                // 1 second (1000ms)
                handler.postDelayed(runnable, 1000)
            }
    
            // This is what initially starts the timer
            handler.postDelayed(runnable, 1000)
    
            // Note that the Thread the handler runs on is determined by a class called Looper.
            // In this case, no looper is defined, and it defaults to the main or UI thread.
        }
        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        fun stopTimer() {
            // Removes all pending posts of runnable from the handler's queue, effectively stopping the
            // timer
            handler.removeCallbacks(runnable)
        }
    }
    
    比较好用的一个快捷键

    多行相同的内容同时编辑时如下:
    AS: control + G
    VS: command + shift + L

    多行编辑
    VS: option + shift + ↓ 为快速复制快捷键
    快速复制

    相关文章

      网友评论

          本文标题:android中的一些小tip(一)

          本文链接:https://www.haomeiwen.com/subject/rlqgzhtx.html