【问题标题】:How to change text background color of a button in Android?如何更改Android中按钮的文本背景颜色?
【发布时间】:2013-09-25 07:25:54
【问题描述】:

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

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

【问题讨论】:

  • 按钮文字的背景还是按钮的文字颜色?
  • setBackgroundResource(R.color.WhateverColorYouLike);改成这样
  • @S.A.NortonStanley 是文本背景色。
  • @khubaib 无论如何设置它的xml方式?
  • 请详细说明您的问题?

标签: android android-button


【解决方案1】:

使用它来设置 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"

【讨论】:

    【解决方案2】:

    也许您应该尝试使用 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>`
    

    【讨论】:

    • 如何设置?请描述更多。
    • 你的意思是怎么设置?
    【解决方案3】:

    在这个的帮助下创建了这个,我已经正确地将 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"/>
    

    【讨论】:

    • @Mansour Fahad 如果对您有帮助,请将其标记为答案。
    • 完美解决方案。谢谢。
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

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

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

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

      【讨论】:

      • 签出此答案@Mansour fahad 如果这解决了您的问题,请将其标记为答案
      • 您能否说得更具体些。我如何将文本放在按钮上?我应该使用 java 方式还是 xml 方式以及如何做?请帮忙。谢谢。
      【解决方案6】:

      您可以为此使用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"

      一切顺利

      【讨论】:

      • 感谢您的帮助,但我想要的是更改文本的背景颜色。
      • 高兴.. 但这是在onClick 期间显示颜色变化的最佳方式。
      猜你喜欢
      • 2013-10-24
      • 2020-11-07
      • 2021-07-05
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 2015-06-04
      • 1970-01-01
      相关资源
      最近更新 更多