Four useful Android development skills, here again

Hello everyone, this article will continue to share some common Android development skills with you, hoping to be helpful to readers.

1. Quickly locate the system version through the stack

This place mainly shares two techniques for you to quickly locate the current system version through the problem stack:

1. Quickly distinguish whether the current system version is below Android 10, or Android 10 and above;

First of all, Android 10 and above introduces a new service Service: ActivityTaskManagerService, ActivityMangerServicesplitting some of the original functions to the former, so when relevant words appear in your question stack ActivityTaskManagerService, it must be Android 10 and above .

You can't find this class in the source code of Android9 and below.

2. Quickly distinguish whether the current system version is below Android 12, or Android 12 and above;

You have to rely on this Looper, let me show you Looperthe source code on Android12:

The core method of Looper to distribute messages loop()will now be forwarded to loopOnce()for processing. This is unique to Android 12 and above, and Looper is a necessary part of Android to process messages. It is the source ancestor of our problem stack, similar to the following:

So this technique is believed to be very necessary: ​​when you see loopOnce() this method from the problem stack, it must be Android12.

2. A fancy way to achieve button spacing

Recently, I saw a new project code and found that the distance between the buttons and between the button and the top and bottom of the project was realized. I used a method that I didn't know before, so I shared it here for everyone to see.

Here is an example of setting the distance between TextView and the top of the screen. The initial effect is as follows:

Next, let's make a step-by-step transformation:

1. First, TextView has a custom xml background:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:height="70dp"
        android:gravity="center_vertical">
        <shape>
            <solid android:color="#ff0000" />
        </shape>
    </item>
</layer-list>

核心就是定义了android:heightandroid:gravity这两个属性,来确保我们自定义背景在组件中的高度及居中位置。

2. 其次将布局中TextView的属性调整下:

  1. 首先height属性一定要调整为wrap_content保证最后TextView按钮的高度的测量最终取minHeight设置的属性值和背景设置的高度这两者的最大值
  1. 其次还要设置minHeight最小高度属性,注意一定要比背景设置的高度值大,保证能和屏幕顶部产生边距效果;
  1. 最后要设置字体的位置为垂直居中,保证字体位置和背景不发生错位

经过上面处理,效果就出来了:

其实上下空白的部分都是属于TextView,设置点击事件也会被响应,这算是其中的缺点之一,当前也可能在业务场景中认为这是一种合理表现。

上面实现的逻辑和TextView的测量逻辑密不可分,感兴趣的同学可以看下这块代码,这里就不带大家进行一一分析了:

三. logcat快速查看当前跳转的Activity类信息

忘了是在哪里看到的了,只要日志过滤start u0,就可以看到每次跳转的Activity信息,非常的有帮助,既不需要改动业务层,也不需要麻烦的安装一些插件啥的。

使用时记得将logcat右边的过滤条件置为,否则你就只能在左边切换到系统进程去看了:

这里我们演示下效果:

1. 跳转到Google浏览器

logcat界面会输出:

会打印一些跳转到包名类名等相关信息。

2. 跳转到系统设置界面

logcat输出:

可以说start u0还是相当好用的。

四. 项目gradle配置最好指向同一本地路径

最近开发中经常存在需要一次性检索多个项目的场景,而这样项目的gradle版本都是相同的,没啥区别。但每打开一个项目就得重新走一遍gradle下载流程,下载速度又是蜗牛一样的慢。

所以强烈建议大家,本地提前准备好几个gradle版本,然后通过设置将项目的gradle指向本地已存在好的gradle:

这样项目第一次打开的速度将是非常快的,而且按道理来说相同gradle版本的项目指向同一本地路径,也可以实现缓存共享。猜的

如果项目好好的编译运行着,突然没网了,可能会提示一些找不到依赖库资源啥的,其实你本地都已经缓存好依赖库资源了,只需要设置下off-mode,不走网络直接通过本地资源编译运行即可

总结

本篇文章主要是介绍了Android开发一些技巧,感觉都是项目中挺常用到的,算是我最近一个月收获的吧,后续准备研究研究compose了,毕竟看到大家们都在搞这个,羡慕的口水都流了一地了哈哈。

历史文章

两个Kotlin优化小技巧,你绝对用的上

Kotlin1.9.0-Beta,它来了!!

Kotlin1.8新增特性,进来了解一下

聊聊Kotlin1.7.0版本提供的一些特性

聊聊kotlin1.5和1.6版本提供的一些新特性

优化@BuilderInference注解,Kotlin高版本下了这些“毒手”!

@JvmDefaultWithCompatibility优化小技巧,了解一下~

Guess you like

Origin juejin.im/post/7250080519069007933