图层列表按钮选择器不填充宽度
编辑: 看起来图层列表是这样绘制的:普通按钮背景 9 块位于后面。高光 9 色块被绘制在正常背景的内部,而不是在其顶部。因此,它进入正常背景的 9 块属性中指定的填充框内。当我修改正常背景上的填充框时,这一点被揭示出来。这不是我想要的。有没有另一种方法,不使用图层列表,让我简单地将突出显示图像分层在按下状态下的正常背景之上?
我有一个按钮背景选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btnprimary" android:state_pressed="false" />
<item android:drawable="@drawable/btn_primary_pressed_bg" android:state_pressed="true" />
</selector>
btnprimary
是一个 9 补丁 .png 文件,这是正常状态。 btn_primary_pressed_bg
是一个图层列表:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/btnprimary"/>
<item android:drawable="@drawable/btnhighlight"/>
</layer-list>
如您所见,按下状态是普通背景,顶部有一个高亮分层以指示按下状态。 btnhighlight
是另一个 9-patch .png,它具有与非按下状态背景完全相同的可拉伸区域和填充框。
这几乎可以完美地工作。问题是,在按下状态下,标准背景正常显示,但顶部的突出显示没有完全填充按钮的宽度(高度被正常填充)。
这是屏幕截图:
这是正常(未按下)状态下的同一个按钮:
有趣的是,如果我将选择器中的图层列表替换为按下状态下的突出显示,它会完全填充宽度:
此模式适用于应用程序中的所有按钮。我没有为按钮指定任何填充。
编辑:这是 slukian 提供的建议的结果:
EDIT: It seems that the layer list is drawing like so: The normal button background 9-patch is at the back. The highlight 9-patch is being drawn inside the normal background, instead of on top of it. So, its going inside the padding box specified in the 9-patch properties of the normal background. This was revealed when I tinkered with the padding box on the normal background. This is not what I want. Is there another way, without using a layer list, for me to simply layer the highlight image over top of the normal background in the pressed state?
I have a selector for a button background:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btnprimary" android:state_pressed="false" />
<item android:drawable="@drawable/btn_primary_pressed_bg" android:state_pressed="true" />
</selector>
btnprimary
is a 9-patch .png file which is the normal state. btn_primary_pressed_bg
is a layer list:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/btnprimary"/>
<item android:drawable="@drawable/btnhighlight"/>
</layer-list>
As you can see, the pressed state is the normal background with a highlight layered on top to indicate the pressed state. btnhighlight
is another 9-patch .png which has exactly the same stretchable area and padding box as the non-pressed state background.
This works almost perfectly. The problem is that in the pressed state, the standard background shows up normally as it should, but the highlight over top does not completely fill the width of the button (the height is filled normally).
Here's a screenshot:
And here's the same button in its normal (not-pressed) state:
Interestingly enough, if I replace the layer-list in the selector with just the highlight on its own for the pressed state, it fills the width exactly:
This pattern applies to all the buttons in the app. I do not have any padding specified for the button.
EDIT: Here's the result of the suggestion provided by slukian:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论