【问题标题】:When I set android background to white, tablayout and menu items background also turned white当我将android背景设置为白色时,tablayout和菜单项背景也变成了白色
【发布时间】:2019-01-02 03:12:53
【问题描述】:

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

styles.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>

colors.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-color android-tablayout menu-items


    【解决方案1】:

    通过在您的样式中设置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">
    

    它应该可以按您的意愿工作。


    编辑:

    如果要设置所有活动的背景,请参见this SO Post

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

    【讨论】:

    • 这就是我的想法,但假设有很多活动,那么在这种情况下,我认为将背景设置为白色并不是一个好主意。没有有效的方法吗?
    • 啊,我不知道你想为所有活动更改它。查看我的答案的编辑信息:)
    • 谢谢,我会试试的,因为我关闭了我的笔记本电脑:)。我觉得是对的。
    • 我有一个问题,我刚刚遇到了 n,我希望我的选项卡布局提升 8dp,但是当我设置 d 属性时没有效果。为什么有任何建议?
    • 它就像一个魅力!谢谢!您能否也看看我的另一个问题是关于视图寻呼机中两个片段之间的通信?你真好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 2015-07-28
    • 2019-07-27
    • 2011-01-15
    • 1970-01-01
    • 2015-08-15
    相关资源
    最近更新 更多