2010-05-01 61 views

回答

320

您需要使用android:背景屬性,如

android:background="@color/white" 

你也需要在strings.xml中

<color name="white">#FFFFFF</color> 

編輯添加值的白色:2012年11月18日

8字母顏色代碼的前兩個字母提供了alpha值,如果您使用的是html 6字母顏色表示法,則顏色是不透明的。

如:

enter image description here

+1

謝謝你現在的作品 – ryan 2010-05-04 02:15:19

+0

你如何指定阿爾法?添加額外的FF等不起作用。 – Jonny 2012-11-14 05:42:48

+0

@Jonny前兩個字母提供了alpha值。 – 2012-11-18 12:18:12

147

您還可以使用

android:background="#ffffff" 

在你的XML佈局或/res/layout/activity_main.xml,或者您可以通過添加

android:theme="@android:style/Theme.Light" 
改變你的AndroidManifest.xml主題

添加到您的活動標籤。

如果要動態改變的背景下,使用

YourView.setBackgroundColor(Color.argb(255, 255, 255, 255)); 
+0

這是正確的答案,因爲它有主題,我相信。單線不是最簡單的方式,而是將整個風格傳播到一切的一種方式。風格是你如何實現一條線甚至是複雜的變化。此外,樣式適用於偏好,不像一般屬性。 (雖然仍然有一些奇怪) – 2017-03-24 14:39:22

55

最簡單的方法

android:background="@android:color/white"

不需要定義任何東西。它使用android.R中的預定義顏色。

+7

和編程:'context.getResources()。getColor(android.R.color.white)' – Paschalis 2013-07-25 22:30:00

+1

謝謝,這實際上是我在找。在我的情況下,我必須用Java來完成。 – Armfoot 2014-08-30 14:16:26

+0

以及支持庫:[鏈接](http://stackoverflow.com/a/31590927/776345) – Paschalis 2016-05-13 22:51:42

3

最簡單的方法是添加android:background="#FFFFFF"到主節點在layout.xml或/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
    <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="10dp" 
     android:textSize="20sp" 
     android:background="#FFFFFF"> 
    </TextView> 
+0

這不是一個好主意,因爲它會導致不必要的GPU透支(首先,窗口背景將被繪製並且然後是TextView的背景)。請參閱:http://www.curious-creature.com/docs/android-performance-case-study-1.html – 2015-06-29 06:28:16

8

要更改最簡單的方式的背景顏色可以通過編程方式(獨佔 - 沒有XML的變化):

LinearLayout bgElement = (LinearLayout) findViewById(R.id.container); 
bgElement.setBackgroundColor(Color.WHITE); 

唯一的要求是,在你的activity_whatever.xml「基地」元素都有,你可以在Java中引用ID(container在這種情況下):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/container" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
    ... 
</LinearLayout> 

Paschalis和詹姆斯在這裏回答說,在檢查了How to set the text color of TextView in code?中的各種可能性之後,我帶着這種解決方案。

希望它可以幫助別人!

0

,你可以嘗試用這種方式

android:background="@color/white" 
3

此方法爲我工作:

​​

集ID在佈局XML

xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/rootLayout" 
android:background="@color/background_color" 

添加顏色值/ color.xml

<color name="bg_color_2">#ffeef7f0</color> 
0

的另一種方式,去佈局 - >您.xml文件 - >設計視圖。然後去你想改變顏色組件樹和選擇佈局。在下面的組件樹中有屬性部分。在屬性部分(圖片部分1)中選擇背景。然後點擊圖片中的第2部分。然後資源窗口將打開。從該選擇顏色菜單。然後選擇你想要的顏色。 enter image description here

2

可以在xml片試試這個:

android:background="@color/background_color" 
0

佈局,改變背景使用此。

android:background="@color/your_color" 

In Program can be use this。對於如:Texview背景顏色

TextView tvName = (TextView) findViewById(R.id.tvName); 
tvName.setBackgroundColor(getResources().getColor(R.color.your_color)); 
0

您可以使用簡單的顏色資源,通常規定內

res/values/colors.xml. 

使用

<color name="red">#ffff0000</color> 

,並通過android:background="@color/red"使用。這種顏色也可用於其他地方,例如作爲文字顏色。參考它在XML以同樣的方式,或通過

getResources().getColor(R.color.red). 

得到它的代碼,您也可以使用任何可繪製資源爲背景,使用android:background="@drawable/mydrawable"這個(這意味着9patch繪項目,正常位圖,形狀可繪製.. )。

0

有些時候,文本與背景具有相同的顏色,請嘗試將android:background =「#CCCCCC」放入列表視圖屬性中,您可以看到它。

0

機器人:背景=「#64B5F6」
您可以後「#」根據自己的規格還是需要根據您希望如何使用它們改變數值。
這是一個示例代碼:

<TextView 
 
     android:text="Abir" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:textSize="24sp" 
 
     android:background="#D4E157" />

謝謝:)

0
  1. 去activity_main.xml中
  2. 有設計視圖/和文本視圖。
  3. 選擇文字
  4. 寫代碼了:

    android:background="@color/colorAccent" 
    
2

使用背景漸變,因爲它不會增加您的應用圖片的應用規模,最好的辦法是Android應用的毒藥所以嘗試使用它少,而不是使用一種顏色作爲背景,您可以在一個背景中使用多種顏色。 enter image description here enter image description here

enter image description hereenter image description here

0

如果您想背景顏色添加到整個活動

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#1de9b6" 
    tools:context="com.example.abc.myapplication.MainActivity"> 
</RelativeLayout> 

如果您想使用後臺視圖

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Andrios" 
    android:background="@color/colorAccent" /> 

希望這可以幫助!

相關問題