Android的按钮的背景颜色改变按钮大小 [英] Android button background color changes button size

查看:784
本文介绍了Android的按钮的背景颜色改变按钮大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是内置的主题我的Andr​​oid应用:

I am using a built-in theme for my Android app:

<style name="AppTheme" parent="android:Theme.Black">
    <!-- Customize your theme here. -->
</style>

我很高兴与这一主题,但我想改变一个按钮的背景颜色。下面是它的外观默认为:

I am happy with that theme, except I want to change the background color of a button. Here is how it looks by default:

下面是当我添加一个背景色为这个按钮会发生什么(的android:背景=@色/ play_bg

Here's what happens when I add a background color to this button (android:background="@color/play_bg"):

咦!它基本上改变了所有的按钮的大小,填充和空间!

Hey!? It basically changed all the button's size, padding and margins!

所以我设法使用 backgroundTint 属性来获得期望的结果(的android:backgroundTint =@色/ play_bg

So I managed to get the expected result using the backgroundTint property (android:backgroundTint="@color/play_bg"):

不幸的是,这仅仅是因为API,这是不是我接受的21版本的支持。

Unfortunately, this is only supported since version 21 of the API, which is not acceptable for me.

于是两个问题:


  • 为什么改变背景混乱与按钮的属性的休息吗?

  • 我怎么没有 backgroundTint
  • 预期的结果
  • Why does changing the background messes with the rest of the button's properties?
  • How do I get the expected result without backgroundTint?

和奖金的问题:我如何才能获得期望的结果编程(我在我的应用程序动态的按钮,所以这将是非常有用的)

And a bonus question: How can I get the expected result programmatically (I have dynamic buttons in my app, so this would be very useful)?

推荐答案

您可以在您的Java文件改变这种颜色。当你的主类加载你可以利用这个按钮的对象,然后改变颜色。

You can change this color in your Java File. When your main class loads you can take an object of this button and then change color.

下面是你如何定义清单文件这个按钮:

Here is how you define this button in Manifest file :

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLAY"
android:id="@+id/btn1"
... />

现在在你的Java文件,当你添加你需要这个XML布局

Now in your Java file when you are adding this XML layout you need to

Button b = (Button)findViewByID(R.id.btn1);
b.getBackground().setColorFilter(0xFFFF0000,PorterDuff.Mode.MULTIPLY);

您也可以使用颜色:
    COLOR.RED
下面有时code不为我工作: -
  b.setBackgroundColor(INT颜色)

You may also use COLOR: COLOR.RED The code below sometimes does not work for me :- b.setBackgroundColor(int color)

这篇关于Android的按钮的背景颜色改变按钮大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆