android中的<selector>和<shape>有什么区别?

What is the difference between <selector> and <shape> in android?

<selector><shape>在android中有什么区别?何时使用 <selector><shape>

What is the difference between <selector> and <shape> in android?

除了被XML用于布局资源之外,它们是无关的。

When to use <selector>, and <shape>?

<shape> 是一种在可绘制资源中定义 a ShapeDrawable 的方法,用于表示具有各种边框和填充的矩形和椭圆形。

<selector> 是一种在可绘制资源中定义 a StateListDrawable 的方法,用于根据应用此 StateListDrawable 的小部件的状态指示要使用的其他可绘制资源。

因此,在按钮的上下文中,<selector> 会说明用于正常背景、按下背景、禁用背景等的内容。 <shape> 将用于提供其中一个州的实际背景。

您可以在 the documentation 中阅读更多关于不同可绘制资源类型的信息。

<selector>元素在XML文件中描述了ColorStateListColorStateList是一个可以应用为颜色的对象,但是会根据状态改变颜色视图。

可用状态:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:color="hex_color"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

<shape> 定义几何形状,包括 Drawable.

的颜色和渐变