【问题标题】:Style displayed in emulator but not on my device模拟器中显示的样式但我的设备上没有
【发布时间】:2016-07-23 14:24:46
【问题描述】:

我在res/values/ 文件夹中关注了style.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:colorBackground">#000000</item>
</style>

以及我的主要活动的以下布局,我使用了style="@style/AppTheme"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    style="@style/AppTheme"                                       // here I've used It.
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.kaushal28.wakeupplease.MainActivity">

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start"
            android:id="@+id/startAlarm" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/stop"
            android:text="Stop"/>
    </LinearLayout>
</RelativeLayout>

我有 Android 4.2.2 Jelly Bean 的设备,模拟器有最新的 Marsh mellow。我认为问题仅在于这种差异。我有这个项目的最低 SDK 版本 15。

我尝试将Build.Gradle 中的targetSDKVersion 更改为15。但它不能解决我的问题。

应该显示的内容:

显示的内容:

我希望背景为黑色。

【问题讨论】:

  • 什么没有被定制?可以分享一些截图吗?
  • @GuilhermeP 看截图

标签: android xml


【解决方案1】:

尝试使用android:windowBackground 而不是android:colorBackground

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@android:color/black</item>
</style>

为什么?

通过您的屏幕,我看到您正在尝试设置窗口背景。所以,最好的属性是android:windowBackground

您的AppTheme 没有覆盖android:windowBackground.. 它只是覆盖android:colorBackground

由于您没有覆盖 android:windowBackground,Android 将使用在您的父主题 (Theme.AppCompat.Light.DarkActionBar) 中定义的颜色。

所以,Android 使用的是:

<item name="colorBackgroundFloating">@color/background_floating_material_light</item>

<color name="background_material_light">@color/material_grey_50</color>
<color name="material_grey_50">#fffafafa</color>

它几乎是白色的。

设备与工作室

老实说..我不完全确定为什么您的设备在模拟器使用它时忽略了android:colorBackground...

可能是 API 版本,或者可能,这只是一种正常行为,因为您的设备会在整个上下文中显示应用于 Activity 的主题。您的设备将始终与 Android Studio 布局预览工具略有不同.

对于任何情况,如果你想发送整个窗口背景,我建议使用android:windowBackground...

android:colorBackground 不仅会影响主窗口的背景,还会影响所有组件,例如对话框,除非您在组件布局中覆盖它。 (此部分取自here

【讨论】:

  • 对于所有参数,我必须在使用样式时找到这样的替代方案?
猜你喜欢
  • 1970-01-01
  • 2014-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多