Android自定义按钮;改变文字颜色

问题描述 投票:0回答:6

我制作了一个可以在不同状态下更改背景可绘制的按钮,这样:

<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 -->
</selector>

这里的问题是,我也尝试像处理可绘制对象一样更改文本颜色,但我无法做到。我已经尝试过 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"
   />

有人知道吗?

android android-button android-selector
6个回答
599
投票

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

<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。然后只需将可绘制对象设置为文本颜色即可:

android:textColor="@drawable/button_text_color"

18
投票

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

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);

5
投票

好的,非常简单,首先去 1. res-value并打开colors.xml 2.复制 1 个定义的文本,例如 #FF4081 并更改名称,例如我更改为白色并更改其值,例如我更改为 #FFFFFF 以获得这样的白色值

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

然后在按钮内添加此行

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

好的 b3 是我的按钮的名称,所以更改按钮的名称,如果您使用白色,则所有其他按钮的名称都将相同,如果您更改不同的颜色,然后将白色更改为您的颜色的名称,但首先您必须在颜色中定义该颜色.xml 就像我在 pont 2 中解释的那样


1
投票

更改按钮的文字颜色

因为此方法现已弃用

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

我使用以下内容:

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

0
投票

像这样使用

getColorStateList

setTextColor(resources.getColorStateList(R.color.button_states_color))

而不是

getColor

setTextColor(resources.getColor(R.color.button_states_color))

0
投票

如果你想改变按钮的背景颜色,你可以去 res/values/themes/themes.xml 并将“Theme.MaterialComponents.DayNight.DarkActionBar”更改为 Theme.AppCompat.Light.NoActionBar

© www.soinside.com 2019 - 2024. All rights reserved.