Android 开发常用控件的小技巧

Posted da_caoyuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 开发常用控件的小技巧相关的知识,希望对你有一定的参考价值。

有段时间没有写过新文章了,今天就把近期学到的一些控件小技巧分享一下。

app:tint="@color/color_34C266" 属性

                 <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:paddingHorizontal="@dimen/dp_8"
                    android:src="@mipmap/icon_scan"
                    app:tint="@color/color_34C266" />

ps:当我们在用 ImageView 显示本地资源图标时,有时会发现本地已经有图标了,但就是没有自己想要的图标颜色,通常我们会找ui再切一个自己想要的颜色图标。这样做就会导致同样的图标,res下会有多个,区别也就是颜色不一样。有了 app:tint 属性,我们就不必如此了,此属性可直接设置图标的颜色。

android:drawableTint="@color/white"

   <TextView
                    style="@style/textStyle14333333"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dp_20"
                    android:drawableStart="@mipmap/icon_red_star"
                    android:drawablePadding="@dimen/dp_5"
                    android:drawableTint="@color/white"
                    android:text="作业" />

ps:有时候我们给 TextView左侧或右侧设置图标,同理没有自己想要的图标颜色时,也可以这样android:drawableTint 设置图标颜色。

LinearLayoutCompat


    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:divider="@drawable/line"
        app:showDividers="end"
        android:orientation="vertical">
        ......
    </androidx.appcompat.widget.LinearLayoutCompat>

line.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#EFEFEF"/>
    <size android:height="@dimen/dp_1"/>
</shape>

ps:当我们在布局文件中,设置分割线的时候,我们通常会用 View 组件,然后设置一个背景色。而用 app:divider 会变得非常简单。

invoke的用法

在 selectAdapter 中定义回调:

 var clickItem: ((position: Int) -> Unit)? = null
 holder.binding.root.setOnClickListener 
         clickItem?.invoke(position)

使用时:

  selectAdapter?.clickItem =  position->

以上是关于Android 开发常用控件的小技巧的主要内容,如果未能解决你的问题,请参考以下文章

Android 开发常用控件的小技巧

Android——隐藏输入法的小技巧

Android Studio和 adb 的一些常用技巧

Android开发中的小技巧

这些 Drawable 的小技巧,你都了解吗?

JS开发中常用的小技巧