android - 当我将 android 背景设置为白色时,tablayout 和菜单项背景也变成了白色

标签 android background-color android-tablayout menu-items

我是安卓新手。我想将应用程序的背景设置为白色,所以我在应用程序主题的样式文件中指定了它。但在这样做之后我发现我的应用程序的选项卡布局和菜单项背景也变成了白色。我不知道该怎么做。试图找到解决方案,但找不到。

样式.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- 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:background">@color/white</item>
    </style>

</resources>

activity_main.xml

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

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

<!-- THIS IS THE VIEW PAGER WIDGET, WHICH IS BASICALLY A CONTAINER OR PARENT FOR FRAGMENTS.
     IT GETS ITS SUPPLIES FROM PAGER ADAPTER. :) -->
<android.support.v4.view.ViewPager
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/viewpager">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        app:tabBackground="@color/colorPrimary">

        <android.support.design.widget.TabItem
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/tab_stock"/>

        <android.support.design.widget.TabItem
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/tab_sale"/>

        <android.support.design.widget.TabItem
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/tab_purchase"/>

    </android.support.design.widget.TabLayout>

</android.support.v4.view.ViewPager>

</LinearLayout>

颜色.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <color name="white">#ffffff</color>

    <!-- COLOR FOR STOCK FRAGMENT' SUB HEADINGS -->
    <color name="colorSubHeading">#03A9F4</color>
    <color name="black">#000000</color>

</resources>

最后我想提一下,我删除了操作栏,而是使用了工具栏。提前致谢。

最佳答案

通过在你的样式中设置 android:background 它会影响(几乎)我猜的一切。只需将 android:background="@color/white" 设置为您的 activity_main.xml 的根 LinearLayout,例如

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/white"
android:orientation="vertical">

它应该会如您所愿地工作。


编辑:

如果要设置所有 Activity 的背景,参见this SO Post .

基本上你创建一个themes.xml,为android:windowBackground 添加一个主题和一个值,然后为你想要的每个 Activity 或整个 Activity 指定主题通过 android:theme="@style/CustomTheme"AndroidManifest.xml 文件中应用。

关于android - 当我将 android 背景设置为白色时,tablayout 和菜单项背景也变成了白色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53997736/

相关文章:

css - 展开与主要内容部分相关的侧边栏的背景颜色

android - 在基于TabLayout的应用程序上实现屏幕旋转

java - 如何在TabLayout中只显示指示器?

安卓。仅当通过 'if statement' 时才滑动到下一个选项卡

Android Material 设计 - 圆角 View 未正确显示圆角阴影

Android - 如何使 RotateAnimation 更流畅和 "physical"?

android - 在 OS X 上运行 ADB shell

css - 背景颜色:透明 在 Safari 和 IE11 浏览器中显示为背景颜色:#0000

javascript - 根据 <td> 中的值着色 <tr>

android - 过滤 SimpleAdapter 时出现 IndexOutOfBoundException