最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】android中通过style的方式去设置EditText的背景色结果不起效果

Android crifan 6883浏览 0评论

【问题】

Android中,已经通过style中,已经搞定了:

【已解决】Android中EditText的style定义中parent的语法(写法)

然后去设置了EditText的背景色:

<color name="NonEditableBackgoundColor">#D9D9D9</color>

<style name="Variable">
    <item name="android:textSize">14sp</item>
    <item name="android:textStyle">normal</item>
</style>

<style name="Variable.Label" parent="@style/Variable">
    <item name="android:textColor">@color/VariableLabelColor</item>
</style>

<style name="Variable.Value" parent="@style/Variable">
    <item name="android:textColor">@color/Black</item>
</style>

<style name="Variable.Value.NonEditable" parent="@style/Variable.Value">
    <item name="android:background">@color/NonEditableBackgoundColor</item>
</style>

然后在代码中通过引用style去设置:

EditText variableValueView = (EditText) variableLayout.findViewById(R.id.variableValue);
variableValueView.setTextAppearance(context, R.style.Variable_Value_NonEditable);

如图:

android edittext var value no background

很明显,变量值的部分,没有所想要的灰色的背景色。

此处希望可以通过上述的style,而使得对应的EditText的背景色设置生效。

【折腾过程】

1.去试了试另外的写法,把背景色直接加到Variable的style中:

    <style name="Variable">
        <item name="android:textSize">14sp</item>
        <item name="android:textStyle">normal</item>
        <item name="android:background">@color/NonEditableBackgoundColor</item>
    </style>

然后再去引用:

			variableValueView.setTextAppearance(context, R.style.Variable);
			//variableValueView.setTextAppearance(context, R.style.Variable_Value_NonEditable);

结果是:

还是无法生效的。

2.后来,倒是的确通过:

    <color name="NonEditableBackgoundColor">#D9D9D9</color>

    <style name="Variable">
        <item name="android:textSize">14sp</item>
        <item name="android:textStyle">normal</item>
    </style>
    
    <style name="Variable.Label" parent="@style/Variable">
        <item name="android:textColor">@color/VariableLabelColor</item>
    </style>
    
    <style name="Variable.Value" parent="@style/Variable">
        <item name="android:textColor">@color/Black</item>
    </style>

    <style name="Variable.Unit" parent="Variable">
    </style>

然后代码中:

			variableValueView.setTextAppearance(context, R.style.Variable_Value);
			variableValueView.setBackgroundResource(R.color.NonEditableBackgoundColor);

是可以设置对应的背景色的:

non editable edittext show gray background

 

ui contain editable and non-editable edittext with gray background or not

 

【总结】

后来的后来,才意识到:

原先对于EditText:

EditText variableValueView = (EditText) variableLayout.findViewById(R.id.variableValue);

来说,只有

android:textSize

android:textStyle

之类的,才算是style,才能在style中定义,然后代码中引用去设置;

而对于背景色等内容来说,不算style:

所以无法在style中定义,然后代码中引用设置;

即使设置了,也无效;

所以单独对于background来说,也有单独的函数:

setBackgroundResource

示例:

(1)定义:

styles.xml中去定义:

<color name="NonEditableBackgoundColor">#D9D9D9</color>

(2)代码中设置:

variableValueView.setBackgroundResource(R.color.NonEditableBackgoundColor);

去设置背景的。

对应的,对于style等显示的内容,是setTextAppearance去决定的。

示例:

(1)定义:

styles.xml中定义:

    <style name="Variable">
        <item name="android:textSize">14sp</item>
        <item name="android:textStyle">normal</item>
    </style>
    
    <style name="Variable.Label" parent="@style/Variable">
        <item name="android:textColor">@color/VariableLabelColor</item>
    </style>
    
    <style name="Variable.Value" parent="@style/Variable">
        <item name="android:textColor">@color/Black</item>
    </style>

(2)代码中去设置:

variableValueView.setTextAppearance(context, R.style.Variable_Value);

即可。

转载请注明:在路上 » 【已解决】android中通过style的方式去设置EditText的背景色结果不起效果

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.170 seconds, using 22.10MB memory