2015-06-21 97 views
2

我试图设置颜色的按钮,但是当我写:setBackgroundColor改变了更多的颜色,然后单击按钮

button.setBackgroundColor(getResources().getColor(R.color.white)); 

按钮变成白色,而且周围的一些空间(我有几个按钮在linearLayout,所以它看起来像一个大的白色按钮)。

任何人都知道如何解决这个问题?

更新:我的XML:

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:gravity="center" 
     android:weightSum="2" 
     android:layout_weight="1" 
     > 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Button" 
      android:background="@android:color/white" 
      android:id="@+id/button1" 
      /> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Button" 
      android:id="@+id/button2"     
      /> 


    </LinearLayout> 

这里左边的按钮看起来更大然后是正确的,因为我改变了它的颜色

+2

请添加您的xml代码! – hasan83

+0

你的按钮的ID在哪里?你如何在Java中初始化它!什么是@ + id/logo我没有在任何地方看到 –

+0

我有时候会阅读你的问题,所以很迷惑你的真正问题是什么? –

回答

1

这是因为一个按钮的默认实现使用自定义绘制为背景,更改背景将覆盖它,并失去所有的花式。

相反,你需要做的是用一种颜色来覆盖现有的背景绘制:

button.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY); 

你也可以找到默认样式的按钮使用,复制和改变颜色有但那会更有用。

+0

您可以告诉我该样式的名称或如何找到它? 我尝试过使用彩色滤镜 - 我认为MULTIPLY模式不起作用 – sagicoh

+0

@ user3142471我试过了,它对API 22上的标准按钮非常适用。样式取决于您使用的主题,但您可能可能通过属性'android.R.style.Widget_Button'找到它。还有[这个问题](http:// stackoverflow。com/q/1521640/3249477)可能有更多的信息。 – Simas

2

你不需要编写代码,您的活动

只是在你的XML:

<Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Button" 
     android:background="#ffffff" 
     android:id="@+id/button1" 
     /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Button" 
     android:id="@+id/button2"     
     /> 

访问本网站的颜色代码:

http://color-hex.com

+0

我需要在代码 中创建按钮,并且仍在xml中编写'android:background =“# “ffffff”'没有改变任何东西 – sagicoh

+1

不,它改变颜色bro.just尝试我的代码它简单,工作正常。您也可以创建color.XML文件并在其中放置颜色代码 – 2015-06-21 08:25:13

0

你尝试

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:gravity="center" 
    android:weightSum="2" 

    > 
    <Button 
     android:layout_weight="1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Button" 
     android:background="@android:color/white" 
     android:id="@+id/button1" 
     /> 
    <Button 
     android:layout_weight="1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Button" 
     android:id="@+id/button2"     
     /> 


</LinearLayout> 

在你parrent布局(LinearLayout)设置weightSum是2,但对孩子在儿童的元素使用android:layout_weight="1"无助

+0

Did not change much – sagicoh

+0

我明白,因为你想有2个按钮与上面的代码相同的大小,对不对?否则,请你告诉我你的预期是什么? –

1

设置你的按钮的高度和宽度如果您使用背景颜色,则不会包装内容。

+0

你也可以设置一些布局的边距,使其与其他人有一定的距离 – qa30m