0

我在我的android应用程序中使用TextView作为Button(平面UI)。下面是代码TextView改变背景色的文字颜色变化

<TextView 
      android:id="@+id/button" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="8dp" 
      android:layout_marginRight="8dp" 
      android:background="@drawable/button_background" 
      android:enabled="false" 
      android:gravity="center" 
      android:paddingBottom="16dp" 
      android:paddingTop="16dp" 
      android:text="Sign Up" 
      android:textColor="@color/white" 
      android:textSize="16sp" 
      android:textStyle="bold" /> 

背景绘制 'button_background' 是

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="#FCD5A5" android:state_enabled="false" /> 
<item android:drawable="#F7941E" /> 

所以当Button启用它应该有深橙色背景否则浅橙色背景。

背景颜色在两种状态(启用和禁用)下都可以正常工作但文本颜色也发生变化。它在启用状态下保持白色,但在禁用状态下变为深灰色。我想在这两个州保持白色。

+0

[试试这个(http://stackoverflow.com/questions/5371719/change-clickable-textviews-color-on-focus-and-click)这个willo deffinately帮助你。 –

回答

0

我目前正在寻找如何为您的选择器做到这一点。但现在,你总是可以做到这一点,并称之为一次初始化,然后当状态变化:

private void updateTextColor(TextView view, Context context) { 
    if (!view.isEnabled()) { 
     view.setTextColor(context.getResources().getColor(android.R.color.white)); 
    } 
} 
0

textcolorselector.xml < =把这个文件在绘制

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

<item android:color="@color/general_blue" android:state_enabled="true"></item> 
<item android:color="@color/general_gray" android:state_enabled="false"></item> 
<item android:color="@color/general_blue"></item> 

将此行添加到color.xml文件

<drawable name="textviewcolor">@drawable/textcolorselector</drawable> 

并最终将此应用于您的l ayout

<TextView 
     android:id="@+id/button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:background="@drawable/button_background" 
     android:enabled="false" 
     android:gravity="center" 
     android:paddingBottom="16dp" 
     android:paddingTop="16dp" 
     android:text="Sign Up" 
     android:textColor="@color/textviewcolor" // <== i made change here! 
     android:textSize="16sp" 
     android:textStyle="bold" /> 
+0

这是向我显示错误“textviewcolor”在colors.xml中不可用。你确定它是可编译的吗? –

+0

对我来说工作正常,更新你的代码color.xml,textcolorselector.xml和layout –