Android Shape中tint与solid的差异及同色设置的作用疑问
Android Shape:
tint vs solid — What’s the Difference? Great question—let’s break down how these two attributes work, why your solid change didn’t show up, and why someone might set them to the same color.
Core Differences
solid: This is the shape’s base fill color. It’s the raw, unfiltered color that fills the shape’s interior before any other effects are applied. Think of it as the shape’s "default" fill layer.tint: This is a color filter applied to the entire drawable (yes, that includessolid,stroke,gradient, and even shape elements like corners—though corners don’t have their own color). It acts like a colored overlay that modifies every pixel of the shape based on the active tint mode (the default issrc_in).
Why Changing solid Had No Visible Effect
In your code snippet:
<shape android:shape="rectangle" android:tint="#ffff00"> <corners android:radius="8dp" /> <size android:height="4dp" /> <solid android:color="#ffff00" /> </shape>
The tint attribute is processed after the solid color is rendered. So even if you change solid to a different color (like #ff0000), the #ffff00 tint filter will override that base fill—turning it yellow. The tint essentially acts as a top-level filter that covers the entire drawable, including the solid layer.
Why Set Them to the Same Color?
There are a few practical scenarios where this setup makes sense:
- Theming & Dynamic Colors: If you plan to swap the tint for a theme-based color later (like
?attr/colorPrimary), keeping a matching or neutralsolidcolor ensures the tint behaves as expected. It’s a way to future-proof the drawable for dynamic theming changes. - Tint Mode Flexibility: The default tint mode is
src_in, but if you switch to modes likemultiplyorscreen, the basesolidcolor affects the final appearance. For example, a darksolidwith a light tint will look different than a whitesolidwith the same tint. Setting them to the same color now keeps things consistent if you adjust the tint mode later. - Legacy Device Support: Before API 21, Android didn’t support the
android:tintattribute on Shape drawables. So setting asolidcolor ensures the shape has a visible fill on older devices, while the tint takes over on newer ones—creating a consistent look across versions. - Testing/Redundancy: It might also just be a leftover from testing. Maybe the developer was experimenting with tint behavior and forgot to remove the matching
solidcolor, or wanted to confirm the tint was applying correctly.
内容的提问来源于stack exchange,提问作者Manuel
相关产品推荐
相关产品推荐

