How to change text background color of a button in Android?

女生的网名这么多〃 提交于 2019-12-10 14:13:46

问题


I have searched a lot but I haven't found what I want. What I’m planning to do is to change the background colour of text on a button. I’m not interested in changing the colour on click. I just want to change the background of the text on a button. Thank you.

Edit: This is a sample. I want to change the black colour if it possible.


回答1:


created this with the help of this,i have correctly positioned the textview on button to acheive this kind of aout put:

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



回答2:


Use this to set the text color of Button :

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

or

android:textColor="@color/white"

Use this to set the background of the button :

setBackgroundResource(R.drawable.button_img);

or

android:background="@drawable/button_img"

or

android:background="@color/blue"



回答3:


Maybe you should try to use RelativeLayout instead of Button. Something like this: `

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



回答4:


if you want to change the color of the button you can use this android:background="#000fff"




回答5:


your question is not correctly asked,but what i understood:

to set the background color of text means create shadow of text on button

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

and setting shadow programmatically

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

to set the background color of button

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

and third thing is onbutton click change background,but you already mentioned that,you don't want that effect.

no you can not do this with only button widget,you have to create two contols:

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



回答6:


You can use selector for this :

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

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"

All the best



来源:https://stackoverflow.com/questions/18998718/how-to-change-text-background-color-of-a-button-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!