实现目标:一个开关按钮, 点击实现文本颜色切换, 解锁后保持状态不变.
以前多在config.xml里面进行主题变量的配置, eg:
1 2 3 4 5 6
| <Group text="锁屏文本颜色"> <NumberChoice text="文本颜色选择" id="color_style" default="0"> <Item value="1" text="白色-适用于深色类壁纸"/> <Item value="0" text="黑色-适用于浅色类壁纸"/> </NumberChoice> </Group>
|
根据这种写法, 就可以在manifest.xml使用@color_style
动态获取到该值. 根据这种写法唯一的缺点就是用户需要打开锁屏自定义设置界面能进行设置, 然鹅大多数用户估计都不知道还有锁屏自定义设置功能存在. 所以现在想在锁屏界面中直接进行变量的控制.
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <var name="circle_x" expression="92" const="true" /> <Button x="32" y="800" w="200" h="100" algin="left" alignV="top"> <Normal> <Rectangle name="rec" algin="left" alignV="top" x="32" y="800" w="200" h="100" cornerRadius="50,50" strokeColor="#ff1e1e1e" fillColor="#4cffffff" weight="4" cap="round" dash="0" strokeAlign="center"> </Rectangle> <Circle visibility="!(#toggle_state)" name="toggle_off" algin="left" alignV="top" x="#circle_x" y="850" r="40" strokeColor="#ffffffff" fillColor="#ff1e1e1e" weight="2" cap="round" dash="0" strokeAlign="center"> </Circle> <Circle visibility="#toggle_state" name="toggle_on" algin="left" alignV="top" x="#circle_x+70" y="850" r="40" strokeColor="#ffffffff" fillColor="#ffefefef" weight="2" cap="round" dash="0" strokeAlign="center"> </Circle> </Normal> <Triggers> <Trigger action="down"> <VariableCommand name="temp" expression="#toggle_state" persist="true" const="true"/> <VariableCommand name="toggle_state" expression="0" condition="eq(#temp,1)" persist="true"/> <VariableCommand name="toggle_state" expression="1" condition="eq(#temp,0)" persist="true"/> <VariableCommand name="vr_value" expression="ifelse(#toggle_state,'#ffffffff','#ff000000')" type="string" const="true" persist="true"/> </Trigger> </Triggers> </Button>
<Text name="vrs" size="32" color="#f03326" x="100" y="400" format="vr_value %s" paras="@vr_value" /> <Text name="vdrs" size="32" color="#f03326" x="100" y="500" format="toggle_state %d" paras="#toggle_state" /> <Text name="rs" size="32" color="#f03326" x="100" y="600" format="toggle_off %d" paras="#toggle_off.visibility" /> <Text name="ras" size="32" color="#f03326" x="100" y="700" format="toggle_on %d" paras="#toggle_on.visibility" />
|