我无法深入研究 android 如何实现其图层列表可绘制对象。但我觉得这很有趣,我几乎不知道为什么会这样。
这里有一些drawables:
九补丁 xml
<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/cam2tst_ripple_bg_img">
</nine-patch>
形状 xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<padding
android:left="@dimen/cam2tst_ripple_horizontal_padding"
android:top="@dimen/cam2tst_ripple_vertical_padding"
android:right="@dimen/cam2tst_ripple_horizontal_padding"
android:bottom="@dimen/cam2tst_ripple_vertical_padding" />
<solid android:color="@android:color/holo_green_dark" />
</shape>
波纹 xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@android:color/holo_green_light">
<item android:drawable="@drawable/cam2tst_ripple_shape"></item>
</ripple>
包含以上所有内容的图层列表
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/cam2tst_ripple_bg_img" />
<item android:drawable="@drawable/cam2tst_ripple_base" />
</layer-list>
不幸的是,我仍然无法在 L 预览中使用我的屏幕截图,但我可以描述它。
我得到的是形状(我显然没有明确设置它的大小)没有覆盖整个 9-patch ! 9-patch 的未拉伸(stretch)部分被神奇地认为是某种“自动填充的东西”。我所期待的(好吧,我期待的正是 android 为我所做的,我的意思是我当时......假设......)是一些不太积极的事情:不是特别大小的形状可绘制覆盖整个九补丁就好像后者是一个普通的 png。
但该形状确实神奇地避开了 9-patch 的未拉伸(stretch)部分,仅覆盖在 9-patch 的拉伸(stretch)部分之上。
这太棒了……但令人困惑,为什么?我可能无法深入挖掘源代码,但这听起来有违直觉(但很好)。虽然我想知道原因。所以我把这个贴在这里。
因为我正在开发一个,所以我将其标记为 android-L。但我认为这应该可以从 Gingerbread 之类的东西开始工作。(只是用其他东西代替波纹可绘制对象,也许是插图可绘制对象等)
最佳答案
这种效果是由两件事共同造成的:
所有九 block 可绘制对象都有一个从内容区域的边缘自动定义的填充区域。内容区域可以使用边界的右线和底线显式定义,也可以从左线和顶线定义的可拉伸(stretch)区域隐式定义。
Layer-list 默认将每一层的 padding 累加到下一层*,有效地将每一层视为上一层的内容。
* Lollipop 推出了新的 attribute用于禁用此行为。
关于android - 为什么Android layer-list drawable如此智能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26727683/