简体   繁体   English

为按钮和图像按钮设置相同的背景色?

[英]Setting same background color to a button and an image button?

I've got a Button object and an Imagebutton object. 我有一个Button对象和一个Imagebutton对象。 And all I want to do is to assign the same background color to both of them. 我要做的就是为他们两个分配相同的背景色。

But the background color of the image button seems always to be brighter than the color of the "ordinary" button?! 但是图像按钮的背景颜色似乎总是比“普通”按钮的颜色亮吗? A lil bit brighter on the emulator and way brighter on my S3 Mini. 模拟器上的亮度稍高一点,而我的S3 Mini上的亮度稍高一点。 Why? 为什么?

private final int BUTTON_BACKGROUND_COLOR_CODE = Color.LTGRAY;

 ...

RelativeLayout TopLayout = (RelativeLayout) findViewById(R.id.topLayout);
TopLayout.removeAllViews();
TopLayout.setPadding(m_TableRowLeftPadding_px, 8, m_TableRowRightPadding_px, 4);

RelativeLayout.LayoutParams bParams = new RelativeLayout.LayoutParams(m_DefaultButtonWidth_px,
    m_CurrentButtonHeight_px);
bParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
bParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);

Button itemAddButton = new Button(this);
itemAddButton.getBackground().setColorFilter(BUTTON_BACKGROUND_COLOR_CODE,
    PorterDuff.Mode.SRC_IN);

itemAddButton.setLayoutParams(bParams);
itemAddButton.setText(m_Resources.getString(R.string.AddItemButtonString));
itemAddButton.setId(ADD_ITEM_BUTTON_ID);
itemAddButton.setOnClickListener(new View.OnClickListener()
{
   ...
});

TopLayout.addView(itemAddButton);

RelativeLayout.LayoutParams ibParams = new RelativeLayout.LayoutParams(MIN_IMG_BUTTON_WIDTH,
    m_CurrentButtonHeight_px);
ibParams.addRule(RelativeLayout.LEFT_OF, itemAddButton.getId());

ImageButton speechButton = new ImageButton(this);

speechButton.setLayoutParams(ibParams);
// speechButton.setImageDrawable(m_Resources.getDrawable(R.drawable.micro2));

speechButton.setContentDescription(m_Resources.getString(R.string.AddSpeechItemString));
speechButton.getBackground().setColorFilter(BUTTON_BACKGROUND_COLOR_CODE,
    PorterDuff.Mode.SRC_IN);

speechButton.setOnClickListener(new View.OnClickListener()
{
  ...
});


TopLayout.addView(speechButton);

@ChristianGraf Remove the color filters and check the original background of the ImageButton and Button: They should differ in brightness. @ChristianGraf删除滤色器,并检查ImageButton和Button的原始背景:它们的亮度应不同。 This implies that the Backgrounds are originally different. 这意味着背景本来是不同的。


How can we solve this? 我们该如何解决呢? We can not; 我们不可以; simply, because the drawables used by system as backgrounds for ImageButton and Button differ in brightness. 很简单,因为系统用作ImageButton和Button背景的可绘制对象的亮度有所不同。 If you try the code on more devices you might notice more/other/less differences. 如果您在更多设备上尝试使用该代码,则可能会发现更多/其他/更少的差异。


One solution could be to set the PorterDuff.Mode in the setColorFilter to SRC or DST. 一种解决方案是将setColorFilterPorterDuff.Mode设置为SRC或DST。 This will actually change the intended outcome. 这实际上将改变预期的结果。


Solution ( Use one of the backgrounds twice ) 解决方法两次使用背景之一

In your code, you first fix the backgrouond of itemAddButton (Button) using this line: 在代码中,您首先使用以下代码行修复itemAddButton (按钮)的背景:

itemAddButton.getBackground().setColorFilter(BUTTON_BACKGROUND_COLOR_CODE,
PorterDuff.Mode.SRC_IN);

Later on, you use the same code to set fix the background of speechButton (ImageButton): 稍后,您将使用相同的代码来设置修复speechButton (ImageButton)的背景:

 
 
 
  
  speechButton.getBackground().setColorFilter(BUTTON_BACKGROUND_COLOR_CODE, PorterDuff.Mode.SRC_IN);
 
  

Now, all you have to do is instead of setting the color filter of the background, let us use the background of your first view (Button): 现在,您要做的就是代替设置背景的彩色滤镜,让我们使用您的第一个视图的背景(按钮):

speechButton.setBackgroundDrawable(itemAddButton.getBackground());

This will ensure that they have identical backgrounds. 这样可以确保他们具有相同的背景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM