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

发布于 2024-10-12 11:25:13 字数 1046 浏览 6 评论 0原文

我制作了一个按钮,可以在不同状态下更改背景可绘制对象,如下所示:

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

有人有线索吗?

I made a button that changes the background drawable on different states, this way:

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

The problem here is that I'm also trying to change the textColor as I do with the drawable but I'm not being able to. I already tried android:textColor and android:color but the first doesn't work whilst the seconds changes my background.

The next code is part of my layout. Regarding to the text color it only works for the normal state text color, thus not changing it to the white one while pressed

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

Has anybody got a clue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

眼泪也成诗 2024-10-19 11:25:13

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

<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"

Create a stateful color for your button, just like you did for background, for example:

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

Place the xml in a file at res/drawable folder i.e. res/drawable/button_text_color.xml. Then just set the drawable as text color:

android:textColor="@drawable/button_text_color"
霊感 2024-10-19 11:25:13

另一种方法是在你的班级中:

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

Another way to do it is in your class:

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);
同尘 2024-10-19 11:25:13

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

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

然后在按钮内添加此行,

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

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

ok very simple first go to
1. res-valuse and open colors.xml
2.copy 1 of the defined text their for example
#FF4081 and change name for instance i changed to white and change its value for instance i changed to #FFFFFF for white value like this

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

then inside your button add this line

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

ok b3 is the name of my button so changed of the name of ur button all others will be same if u use white color if you change different color then change white to the name of your color but first you have define that color in colors.xml like i explained in pont 2

夏有森光若流苏 2024-10-19 11:25:13

更改按钮的文本颜色

因为此方法现已弃用,所以

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

我使用以下方法:

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

Changing text color of button

Because this method is now deprecated

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

I use the following:

button.setTextColor(ContextCompat.getColor(mContext, R.color.your_color));
感情洁癖 2024-10-19 11:25:13

像这样使用 getColorStateList

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

而不是 getColor

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

Use getColorStateList like this

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

instead of getColor

setTextColor(resources.getColor(R.color.button_states_color))
明明#如月 2024-10-19 11:25:13

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

if you want to change the background color of button, you can go res/values/themes/themes.xml and change Theme.MaterialComponents.DayNight.DarkActionBar to Theme.AppCompat.Light.NoActionBar

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文