[英]XML preview color is different from the actual app anddroid
I've created a project and added 2 buttons in activity_main.xml but they have different colors in layout preview and the actual app.我创建了一个项目并在 activity_main.xml 中添加了 2 个按钮,但它们在布局预览和实际应用程序中具有不同的 colors。 How come?怎么来的?
There are 2 files under themes:主题下有2个文件:
themes.xml:主题.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="NecklaceShowcase" parent="Theme.MaterialComponents.Light.NoActionBar" >
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
and themes.xml (night)和主题。xml(夜)
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="NecklaceShowcase">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_200</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
The layout preview is using the theme you have set in your themes.xml file.布局预览使用您在themes.xml 文件中设置的主题。
When you run the app the btn2
is switching to red because in your activity_main.xml
it is setting its background to: halo_red_light
.当您运行该应用程序时, btn2
将切换为红色,因为在您的activity_main.xml
中,它将其背景设置为: halo_red_light
。
I'm unable to tell why the other button is changed to grey this may have been set in your activity class.我不知道为什么另一个按钮变为灰色,这可能已在您的活动 class 中设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.