2017-02-25 10 views
1

我有2個主題,一個黑暗和明亮的主題。我想當前主題的背景顏色,並用它在佈局文件是這樣的:在佈局文件中使用當前主題backgroundcolor

<View 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="(backgroundColor)" /> 

比方說(backgroundColor)是當前的主題背景顏色。我必須在那裏放置什麼,而不是(backgroundColor)

+0

的源代碼這是什麼!!這個'(backgroundColor)'假設是什麼意思? –

+0

一個佔位符爲背景顏色,我無法獲得 –

+0

我仍然不明白你的問題,但是你可能想看到這個[回答](http://stackoverflow.com/questions/7378636/setting -background-color-of-android-layout-element) –

回答

1

使用android:theme

<View 
    android:theme="@style/yourTheme" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

和你的價值觀\ styles.xml

<style name="yourTheme" parent="AppTheme"> 
     <item name="android:background">@color/green</item> 
</style> 

編輯:請儘量在第一時間發佈完整的問題,如果你做出改變嘗試將其添加到編輯部分下。否則,愛你的人會遇到麻煩。

爲您編輯的問題已經有答案here

您可以得到當前主題 背景顏色(或繪製對象):

TypedValue a = new TypedValue(); 
getTheme().resolveAttribute(android.R.attr.windowBackground, a, true); 
if (a.type >= TypedValue.TYPE_FIRST_COLOR_INT && a.type <= TypedValue.TYPE_LAST_COLOR_INT) { 
    // windowBackground is a color 
    int color = a.data; 
} else { 
    // windowBackground is not a color, probably a drawable 
    Drawable d = activity.getResources().getDrawable(a.resourceId); 
} 

並將其設置爲你的視圖!

+0

抱歉給您帶來不便。我很抱歉地告訴你,但我不能在這個問題上編碼,因爲它會把我的佈局和代碼變成一團糟。不管怎樣,謝謝你 –

0

您的問題的正確答案可能是?android:colorBackground。但在某些場合,您可能仍然想使用?android:windowBackground

雖然windowBackground將作爲您的活動背景,但colorBackground看起來像默認背景顏色爲您的內容。

你的情況,你最終會像下面這樣:

<View 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="?android:colorBackground" /> 

對於那些「默認」爲主題的屬性的完整列表,你可以檢查出的基本Android themes.xml

相關問題