首页 文章

Android定制按钮;改变文字颜色

提问于
浏览
226

我做了一个按钮,改变了不同状态下的背景,这样:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true" android:drawable="@drawable/btn_location_pressed" /> <!-- pressed -->
 <item android:state_focused="true" android:drawable="@drawable/btn_location_pressed"/> <!-- focused -->
 <item android:drawable="@drawable/btn_location"/> <!-- default -->

这里的问题是我也试图改变textColor,就像我对drawable一样,但我无法做到 . 我已经尝试过android:textColor和android:color但第一个不起作用,而秒数改变了我的背景 .

下一个代码是我布局的一部分 . 关于文本颜色,它仅适用于正常状态文本颜色,因此在按下时不会将其更改为白色

<Button android:id="@+id/location_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:background="@drawable/location"          
        android:textSize="15sp"
        android:textColor="@color/location_color"
        android:textColorHighlight="#FFFFFF"
   />

有人有线索吗?

4 回答

  • 16

    为您的按钮创建有状态颜色,就像您为背景所做的那样,例如:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <!-- Focused and not pressed -->
        <item android:state_focused="true" 
              android:state_pressed="false" 
              android:color="#ffffff" />
    
        <!-- Focused and pressed -->
        <item android:state_focused="true" 
              android:state_pressed="true" 
              android:color="#000000" />
    
        <!-- Unfocused and pressed -->
        <item android:state_focused="false" 
              android:state_pressed="true" 
              android:color="#000000" />
    
        <!-- Default color -->
        <item android:color="#ffffff" />
    
    </selector>
    

    将xml放在res / drawable文件夹中的文件中,即res / drawable / button_text_color.xml . 然后将drawable设置为文本颜色:

    android:textColor="@drawable/button_text_color"
    
  • 1

    另一种方法是在你的课堂上:

    import android.graphics.Color; // add to top of class  
    
    Button btn = (Button)findViewById(R.id.btn);
    
    // set button text colour to be blue
    btn.setTextColor(Color.parseColor("blue"));
    
    // set button text colour to be red
    btn.setTextColor(Color.parseColor("#FF0000"));
    
    // set button text color to be a color from your resources (could be strings.xml)
    btn.setTextColor(getResources().getColor(R.color.yourColor));
    
    // set button background colour to be green
    btn.setBackgroundColor(Color.GREEN);
    
  • 531

    确定非常简单首先转到1. res-valuse并打开colors.xml 2.copy定义文本1例如#FF4081并更改名称例如我改为白色并更改其值例如我改为#FFFFFF for像这样的白色值

    <color name="White">#FFFFFF</color>
    

    然后在你的按钮里面添加这一行

    b3.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.White));
    

    ok b3是我的按钮的名称,所以如果你使用白色,如果你改变不同的颜色,然后将白色改为你的颜色的名称,但是首先你用颜色定义了这个颜色的名称 . 像我在pont 2中解释的xml

  • 3

    Changing text color of button

    因为此方法现已弃用

    button.setTextColor(getResources().getColor(R.color.your_color));
    

    我使用以下内容:

    button.setTextColor(ContextCompat.getColor(mContext, R.color.your_color));
    

相关问题