2012-02-06 65 views
4

設置背景色的TextView在我的應用程序,我想設置一個風格,以TextView這將使TextView看起來像一個標題爲表現在以下figure-的Android在styles.xml

"General" is the heading in this image.

當我應用styles.xml(如下所示)的樣式時,它會應用指定的字體和字體顏色。但是,它不適用白色背景。我們可以做到這一點嗎?

定義的風格就像是這個 -

<?xml version="1.0" encoding="utf-8"?> 

<style name="settings_header"> 
    <item name="android:layout_marginBottom"> 10dip </item> 
    <item name="android:background"> @color/white </item> 
    <item name="android:paddingLeft"> 10dip </item> 
    <item name="android:layout_width"> match_parent </item> 
    <item name="android:layout_height"> wrap_content </item> 
    <item name="android:textSize"> 22sp </item> 
    <item name="android:textColor"> @color/black </item> 
    <item name="android:textStyle"> bold </item> 
</style> 

它適用的風格很像

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
textview = (TextView) findViewById(R.id.textView); 
textview.setTextAppearance(ScrSettings.this,R.style.settings_header); 
} 
+0

您確定要在白色背景上使用白色文字顏色嗎? :) – Jin35 2012-02-06 10:34:23

+0

顏色只是一個例子。顏色可以是任何顏色。但事情是一個彩色背景和一些文字。無論如何,編輯這個問題。謝謝。 – Rajkiran 2012-02-06 11:21:07

回答

1

最後我找到了一個辦法。

textview.setTextAppearance(context, R.style.settings_header); 
textview.setBackgroundResource(R.color.white); 

的問題仍然沒有得到充分的回答,背景設置爲TextView兩次是不是我們想要的。我們已經在styles.xml中設置了背景。

0

我現在如果不將其代碼他LP

試試這個 @色/#FFF

,而不是「白」

好運

+0

@ color /#fff不是一個合適的值。它甚至不會編譯。甚至'@ color /#ffffff'都是錯誤的;因爲當我們說@ color/[name]時,它意味着我們正在調用一個名爲[name]的元素。所以這不是我問題的答案。 – Rajkiran 2012-02-06 11:10:18

0

,如果你沒有在你的水庫顏色的文件夾,那麼你需要更換@色/白與#FFFFFF

+0

我已經定義了colors.xml,其中我有一個白色,具有相同的值。我甚至試着把'#ffffff'代替'@ color/white',但我仍然看不到背景。 – Rajkiran 2012-02-06 11:06:13

0

公共無效setTextAppearance(上下文語境,INT渣油)

設置的文本顏色大小風格一絲色彩,並從指定TextAppearance資源高亮顏色

這是setTextAppearance的實際輸出。 爲什麼你在考慮背景顏色而沒有閱讀 setTextAppearance 方法的行爲?

如果您想要在styles.xml中設置的所有屬性styles.xml僅在XML文件中使用style="@style/settings_header"

我希望你明白這一點。

EDIT

要設置在運行時使用setBackgroundColor方法背景顏色。

+0

其實我想在運行時決定'TextView'(Header或Subheader)的樣式。我甚至嘗試過'style =「@ style/settings_header',它工作正常,但我不希望它是staic,即在xml中,並且想要定義它的運行時間,如上面的代碼所示。在運行時設置背景顏色? – Rajkiran 2012-02-06 12:06:19

+0

檢查更新的答案 – 2012-02-06 12:14:36

+0

不幸的是,即使'setBackgroundColor()'不工作,但'setBackgroundResource(R.color.white)'正在工作,但是從xml設置背景資源也應該工作。 ,發佈這個答案。謝謝。 – Rajkiran 2012-02-06 12:34:42

相關問題