【问题】
Android中,已经通过style中,已经搞定了:
然后去设置了EditText的背景色:#D9D9D9
14sp
normal
@color/VariableLabelColor
@color/Black
@color/NonEditableBackgoundColor
然后在代码中通过引用style去设置:EditText variableValueView = (EditText) variableLayout.findViewById(R.id.variableValue);
variableValueView.setTextAppearance(context, R.style.Variable_Value_NonEditable);
如图:
很明显,变量值的部分,没有所想要的灰色的背景色。
此处希望可以通过上述的style,而使得对应的EditText的背景色设置生效。
【折腾过程】
1.去试了试另外的写法,把背景色直接加到Variable的style中:
14sp
normal
@color/NonEditableBackgoundColor
然后再去引用:variableValueView.setTextAppearance(context, R.style.Variable);
//variableValueView.setTextAppearance(context, R.style.Variable_Value_NonEditable);
结果是:
还是无法生效的。
2.后来,倒是的确通过:#D9D9D9
14sp
normal
@color/VariableLabelColor
@color/Black
然后代码中:variableValueView.setTextAppearance(context, R.style.Variable_Value);
variableValueView.setBackgroundResource(R.color.NonEditableBackgoundColor);
是可以设置对应的背景色的:
【总结】
后来的后来,才意识到:
原先对于EditText:EditText variableValueView = (EditText) variableLayout.findViewById(R.id.variableValue);
来说,只有
android:textSize
android:textStyle
之类的,才算是style,才能在style中定义,然后代码中引用去设置;
而对于背景色等内容来说,不算style:
所以无法在style中定义,然后代码中引用设置;
即使设置了,也无效;
所以单独对于background来说,也有单独的函数:
setBackgroundResource
示例:
(1)定义:
styles.xml中去定义:#D9D9D9
(2)代码中设置:variableValueView.setBackgroundResource(R.color.NonEditableBackgoundColor);
去设置背景的。
对应的,对于style等显示的内容,是setTextAppearance去决定的。
示例:
(1)定义:
styles.xml中定义:
14sp
normal
@color/VariableLabelColor
@color/Black
(2)代码中去设置:variableValueView.setTextAppearance(context, R.style.Variable_Value);
即可。