2015-06-21 102 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