问题
In the real world, the example control panel layout (below) appears within its own FrameLayout (Fragment container) and (depending on whether the app is running on a tablet) alongside a map, whose colours are set by a colorpicker fired by the 'colour' buttons. (NB: The map and the control panel have different backgrounds.)
On return from the colorpicker the associated map particle and the Button background are updated with the new color.
The idea is that (as observed by the user) the colour of the button exactly matches the map.
And of course, it does not! (Unless the picked color has 255-alpha).
I know this is 'expected behaviour' but is there a clever (or 'hacky') trick that will obliterate the parent background (behind the Button child only) so that the child background color is correctly rendered?
Example Control Panel Layout and code
<LinearLayout android:id="@+id/settingsLL"
android:orientation="vertical">
<LinearLayout android:id="@+id/oneOfMany"
android:orientation="horizontal">
<TextView style="@style/wptedit_title" android:text="[some title]" />
<Button style="@style/wptedit_color" android:id="@+id/colorBtn" />
</LinearLayout>
</LinearLayout>
// Control panel background
settingsLL.setBackgroundColor(0xFFD5FFDD);
// Faint red tint gives desired map background not reflected by button
colorBtn.setBackgroundColor(0x04FF0000);
回答1:
A better question would have been:
How do I combine the alpha-255 background color of the map with any other colour to produce a new alpha-255 color which can then be applied to the control panel button such that the appearance of the control panel button perfectly corresponds to the map, irrespective of control panel background?
So far, so good! (See android.support.v4.graphics.ColorUtils)
colorBtn.setBackgroundColor(ColorUtils.compositeColors(pickedColor, mapBackgroundColor);
See here for the doco. The source wasn't in my SDK. I had to get it here.
来源:https://stackoverflow.com/questions/41579456/ignore-parent-view-background