2017-02-25 9 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

相关问题