33

我是android开发的初学者。我最近在看其他一些代码并喜欢一些功能view.onApplyWindowInsets(windowInsets), windowInsets.getSystemWindowInsetTop()。这个词在同一个应用程序中被一次又一次地使用。

我试着用谷歌搜索它并找到InsetDrwable了有解释的课程

将另一个 Drawable 插入指定距离的 Drawable。当 View 需要的背景小于 View 的实际边界时使用此选项。

有人可以解释一下上面Insets的代码是什么意思吗?

一个例子的解释将不胜感激。

4

1 回答 1

50

假设您有一个 TextView,并且需要为 TextView 添加背景。但是,另一方面,您不希望背景扫描整个 View (TextView),因此,您需要使用 Insets。

将另一个 Drawable 插入指定距离或内容边界的一部分的 Drawable。当 View 需要的背景小于 View 的实际边界时使用此选项。

背景可以是可绘制的。因此,您需要使用<inset>xml 文件 (activity_main.xml) “例如”中的属性。

然后,在使用<inset>标签之后,您可以指定一些属性,例如:

<inset
    android:drawable="@drawable/(Enter the file name under drawable)"
    android:insetBottom="4dp"
    android:insetTop="4dp"/>

有关更多信息,请查看Android developer.com上的InsetDrawable

希望这可以帮助!

于 2016-10-12T07:04:12.027 回答