如何在Android中更改按钮的文本背景颜色?

时间:2013-09-25 07:25:55

标签: android android-button

我搜索了很多,但我找不到我想要的东西。我打算做的是改变按钮上文字的背景颜色。我对改变点击颜色不感兴趣。我只想更改按钮上文本的背景。谢谢。

编辑: 这是一个样本。如果可能,我想改变黑色。

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

6 个答案:

答案 0 :(得分: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"

答案 1 :(得分: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>`

答案 2 :(得分:2)

enter image description here

借助于此创建了这个,我已经正确地将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"/>

答案 3 :(得分:1)

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

答案 4 :(得分: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点击更改背景,但你已经提到过,你不想要这种效果。

enter image description here

不,你不能只使用按钮小部件,你必须创建两个控制:

    button with textView:
set textview background color
 or 
    Image with textView:
set textview background color
 or
create [coumpound control][2]

答案 5 :(得分:0)

您可以使用selector

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

一切顺利