5

我已经搜索了很多,但我没有找到我想要的。我打算做的是更改按钮上文本的背景颜色。我对单击时更改颜色不感兴趣。我只想更改按钮上文本的背景。谢谢你。

编辑:这是一个示例。如果可能的话,我想改变黑色。

http://i.imgur.com/B8tYnEN.png

4

6 回答 6

3

使用它来设置 Button 的文本颜色:

setTextColor(getResources().getColor(R.color.white));

或者

android:textColor="@color/white"

使用它来设置按钮的背景:

setBackgroundResource(R.drawable.button_img);

或者

android:background="@drawable/button_img"

或者

android:background="@color/blue"
于 2013-09-25T07:42:44.737 回答
3

也许您应该尝试使用 RelativeLayout 而不是 Button。像这样的东西:`

<RelativeLayout
    android:id="@+id/RelativeLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:clickable="true"
    android:background="#ffffff" >
  <TextView
        android:id="@+id/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text"
        android:textColor="#ffffff"
        android:background="#000000"
        android:textSize="16sp" />
</RelativeLayout>`
于 2013-09-25T08:21:45.050 回答
2

在此处输入图像描述

在这个的帮助下创建了这个,我已经正确地将 textview 放置在按钮上以实现这种输出:

<Button
        android:id="@+id/button1"
        android:layout_width="250dp"
        android:layout_height="150dp"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="380dp"
        />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="130dp"
        android:layout_height="70dp"
        android:layout_marginLeft="80dp"
        android:layout_marginTop="420dp"
        android:textSize="40dp"
        android:text="Text"
        android:gravity="center"
        android:textColor="#ffff00"
        android:background="#000000"/>
于 2013-09-25T09:31:31.217 回答
1

如果你想改变按钮的颜色,你可以使用这个 android:background="#000fff"

于 2013-09-25T07:34:27.983 回答
1

您的问题没有正确提出,但我理解的是:

设置文本的背景颜色意味着在按钮上创建文本的阴影

<style name="shadowed_button">
    <item name="android:shadowColor">#444444</item>
    <item name="android:shadowDx">1</item>
    <item name="android:shadowDy">1</item>
    <item name="android:shadowRadius">9</item>
</style>

并以编程方式设置阴影

button.setShadowLayer(9, 1, 1, Color.rgb(44, 44, 44));

设置按钮的背景颜色

<Button
      android:id="@+id/street_btn"
      android:layout_width="wrap_content"
      android:background="@drawable/layout_a" > <!-- your required color -->
    </Button>

         or

button.setBackgroundColor(Color.BLUE);

第三件事是onbutton点击改变背景,但你已经提到过,你不想要那种效果。

在此处输入图像描述

不,您不能仅使用按钮小部件来执行此操作,您必须创建两个控件:

    button with textView:
set textview background color
 or 
    Image with textView:
set textview background color
 or
create [coumpound control][2]
于 2013-09-25T07:35:54.997 回答
0

您可以为此使用选择器

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
    <item android:state_focused="true" android:state_pressed="true" android:color="#FF0000" />
    <item android:state_focused="false" android:state_pressed="true" android:color="#FF0000" />
    <item android:color="#ffffff" />
</selector>

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

android:textColor="@drawable/button_text_color"

祝一切顺利

于 2013-09-25T07:39:06.083 回答