【问题标题】:How can I change the colour of a Button without changing its size?如何在不更改大小的情况下更改按钮的颜色?
【发布时间】:2017-03-14 13:24:19
【问题描述】:

我是 android 开发的新手,在更改按钮颜色时遇到问题。

 <Button
            android:id="@+id/SignUp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Sign Up"
            android:textColor="#ff0000" />
        <Button
            android:id="@+id/SignIn"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:background="#00ff00"
            android:layout_height="wrap_content"
            android:text="Sign In"
            android:textColor="#ff0000" />

这是我遇到问题的按钮。我更改了背景颜色,但它改变了按钮的大小。

左键是我希望我的按钮看起来的样子,但在更改颜色后,我得到的是右键。

我快速浏览了一下以尝试找到解决方案,但我发现的只是告诉我更改我已经完成的背景颜色。

那么我怎样才能让它看起来像左键呢?我假设它与drawables有关,但老实说我不知道​​我在寻找什么。任何能指引我正确方向的指南或任何东西都会很棒。

谢谢!

【问题讨论】:

  • 提供两个按钮代码
  • 你也需要显示其他按钮代码
  • 对不起,完成!谢谢

标签: android xaml xamarin visual-studio-2017


【解决方案1】:

最简单的答案,使用backgroundTint insted background 此代码将起作用:

<Button
android:id="@+id/SignIn"
android:layout_width="0dp"
android:layout_weight="1"
android:backgroundTint="#00ff00"
android:layout_height="wrap_content"
android:text="Sign In"
android:textColor="#ff0000" />

【讨论】:

    【解决方案2】:

    背景属性可以不仅仅是一种颜色。您正在用纯色替换默认图像。选项是制作自己的图像,或调整高度和宽度属性。

    https://developer.android.com/guide/topics/ui/controls/button.html#Style

    【讨论】:

    • 谢谢,希望我能接受两个答案,指导让我更好地理解它是如何工作的。
    【解决方案3】:

    你可以把你的按钮放到一个水平方向的LinearLayout视图中,然后设置按钮的权重为1,宽度为0dp,linearLayout的高度为40dp。

    【讨论】:

      【解决方案4】:

      您在这里所做的是将图像背景替换为纯色。您应该改用样式。

      请试试这种风格

      <style name="SpecialButton" parent="Widget.AppCompat.Button.Colored">
         <item name="colorButtonNormal">#00ff00</item>
         <item name="android:textColorPrimary">#ff0000</item>
      </style>
      

      在您的styles.xml 和

      <style name="SpecialButton" parent="Widget.AppCompat.Button.Colored">
         <item name="android:colorButtonNormal">#00ff00</item>
         <item name="android:textColorPrimary">#ff0000</item>
      </style>
      

      在您的 v21/styles.xml 和

      并将此样式应用于按钮。如果这对您有用,请发表评论,因为我自己没有测试过。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-24
        • 2019-05-29
        • 2021-07-18
        • 1970-01-01
        • 1970-01-01
        • 2021-06-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多