【背景】
对于Android中的字符的属性设置,已经知道可以静态的在xml中去定义。
现在希望实现,可以动态的,在代码中去设置属性和样式。
【解决过程】
1.搜:
android set text style
找到:
java – Android – set TextView TextStyle programmatically? – Stack Overflow
->
java – How to change a TextView’s style at runtime – Stack Overflow
2.然后基本搞定了:
(1)将需要动态设置的样式放到资源里面
在/res/values下面新建xml配置文件:
/res/values/themeConfig.xml
内容为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | < color name = "gradientStartColor" >#87CEFA</ color > <!-- <color name="GroupSpliterColor">#BEBDDE</color> --> <!-- <color name="GroupSpliterColor">#32A6C9</color> --> < color name = "GroupSpliterColor" >#2A2598</ color > < style name = "VariableName" > < item name = "android:textColor" >@color/MMBlue1</ item > < item name = "android:textSize" >14sp</ item > < item name = "android:textStyle" >normal</ item > </ style > < style name = "VariableValue" > < item name = "android:textSize" >14sp</ item > < item name = "android:textStyle" >normal</ item > </ style > </ resources > |
(2)xml中已有TextView的基本定义
/res/layout/variable_item.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "match_parent" android:layout_height = "match_parent" android:paddingLeft = "20dp" android:orientation = "horizontal" > < TextView android:id = "@+id/variableName" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:gravity = "center" android:paddingLeft = "5dp" android:textColor = "@color/MMBlue1" android:textSize = "14sp" /> < TextView android:id = "@+id/variableValue" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:gravity = "center" android:paddingLeft = "5dp" android:textSize = "14sp" /> </ LinearLayout > |
(3)代码中就可以动态调用样式资源去配置TextView的样式了
1 2 3 4 5 6 7 | TextView variableNameView = (TextView) variableLayout.findViewById(R.id.variableName); variableNameView.setText(name); variableNameView.setTextAppearance(context, R.style.VariableName); TextView variableValueView = (TextView) variableLayout.findViewById(R.id.variableValue); variableValueView.setText(value.toString()); variableNameView.setTextAppearance(context, R.style.VariableValue); |
效果还是不错的:
3.后来对于配置文件和代码,都更新升级为:
(1)/res/values/themeConfig.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | < color name = "gradientStartColor" >#87CEFA</ color > <!-- <color name="GroupSpliterColor">#BEBDDE</color> --> <!-- <color name="GroupSpliterColor">#32A6C9</color> --> < color name = "GroupSpliterColor" >#2A2598</ color > < color name = "VariableName" >#2A2598</ color > < style name = "Variable" > < item name = "android:textSize" >14sp</ item > < item name = "android:textStyle" >normal</ item > </ style > < style name = "Variable.Name" > < item name = "android:textColor" >@color/VariableName</ item > </ style > < style name = "Variable.Value" > </ style > < style name = "Variable.Unit" > </ style > </ resources > |
(2)代码中:
1 2 3 4 5 6 7 8 9 10 | TextView variableNameView = (TextView) variableLayout.findViewById(R.id.variableName); variableNameView.setText(name); variableNameView.setTextAppearance(context, R.style.Variable_Name); TextView variableValueView = (TextView) variableLayout.findViewById(R.id.variableValue); variableValueView.setText(value.toString()); variableNameView.setTextAppearance(context, R.style.Variable_Value); TextView variableUnitView = (TextView) variableLayout.findViewById(R.id.variableUnit); variableUnitView.setText(unit.toString()); variableUnitView.setTextAppearance(context, R.style.Variable_Unit); |
【总结】
想要动态在代码中配置字符的样式,主要步骤就是,将样式单独弄成对应的资源resource放在/res/values/xxx.xml中,然后动态在代码中通过setTextAppearance传入对应的R.style.yyy即可。
转载请注明:在路上 » 【已解决】Android中动态设置字符的属性和样式style