首页 > 解决方案 > 当应用程序在回压后恢复时,布局元素的背景颜色会发生变化

问题描述

我有两个片段的活动。Activity 仅包含 TabLayout (2tabs),这两个片段填充了选项卡主体。

当我第一次启动应用程序时,一切正常,所有颜色都从colors.xml. 当我按下backpress按钮(在 2 个模拟器和真实设备中尝试过,API 从 23 到 28)并重新打开应用程序时,(#222222)活动选项卡的颜色以及片段中(#222222)其他元素的颜色都会变为原色colorAccent: @color/green我的情况。并且这个green颜色是正常加载的。应用程序中的其他颜色根本不会改变,它们保持原样。

我花了 6 个小时调试这个问题,我不知道为什么其他颜色保持原样。

我已经尝试并添加了一个新活动,当我单击 中的按钮时开始,fragment我将此活动设置为与我的as中定义background color的相同。#222222colors.xml<color name="darkg">#222222</color>

此活动的背景始终为green,即使在我正常启动应用程序(未恢复)而活​​动选项卡按钮为darkg彩色时也是如此。

这就是我从他们的父活动开始我的 framgent 的方式:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_inside);

    fm = getSupportFragmentManager();

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(fm);

    // Set up the ViewPager with the sections adapter.
    mViewPager = findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return watchList;
            case 1:

                return exbook;
            default:
                return null;
        }
    }`

我试图删除一些我认为会导致这个问题的依赖项,我已经清理了很多次项目,我已经使缓存无效并重新启动了 android studio,但没有任何帮助。

Android 5.0 支持我的应用程序,我尝试在具有 API 23 的模拟器上对其进行测试,选项卡颜色在那里没有改变,只有片段中的其他元素。另一方面,我在具有 API 28 和真实设备 API 28 的模拟器上进行了测试,并且选项卡按钮也与片段中的其他元素一起变为绿色 + 该活动始终为绿色。

我还应该提到,在我的片段中,我有recyclerview并且我有这样的alternate rows背景颜色设置(它设置在 中Adapter.class,并且这种颜色始终有效,永远不会变为绿色:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    //holder.textViewPos.setText("#" + (position + 1));
    holder.swipeLayout.setOffset(itemsOffset[position]);
    //((RecyclerView.LayoutParams) holder.itemView.getLayoutParams()).height = 300;
    if(position %2 == 1)
    {
        holder.itemView.setBackgroundColor(Color.parseColor("#222222"));

        //  holder.imageView.setBackgroundColor(Color.parseColor("#FFFFFF"));
    }
    else
    {
        holder.itemView.setBackgroundColor(Color.parseColor("#2B2B2B"));
        //  holder.imageView.setBackgroundColor(Color.parseColor("#FFFAF8FD"));
    }
}

活动的 XML 文件,我有TabLayout

<androidx.coordinatorlayout.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".InsideActivity">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:background="#fff">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="15dp"
            android:layout_weight="1"
            app:layout_constraintVertical_bias="1.0"
            app:srcCompat="@drawable/logo" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="65dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:tabBackground="@drawable/tab_color_selector"
        app:tabIndicator="@color/darkg">

        <com.google.android.material.tabs.TabItem
            android:id="@+id/tabItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Watchlist" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/tabItem2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Exbook" />

    </com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>

<com.me.appka.NonSwipeableViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
</com.me.appka.NonSwipeableViewPager>

片段 XML:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
    android:id="@+id/addToWatchlist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/darkg"
    android:fontFamily="sans-serif"
    android:gravity="center_vertical"
    android:includeFontPadding="true"
    android:onClick="onClickAddNewPerson"
    android:text="ADD NEW"
    android:textAlignment="center"
    android:textColor="@color/details_text"
    android:textSize="20dp" />

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_below="@id/addToWatchlist"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</RelativeLayout>

单击按钮时在 Fragment 中启动的活动 XML addToWatchlist

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddToWatchlistActivity"
android:background="@color/darkg">

<TextView
    android:id="@+id/watchlistTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="sans-serif"
    android:text="Add new person to you Watchlist"
    android:textAlignment="center"
    android:textColor="@android:color/white"
    android:textSize="18dp"
    app:layout_constraintBottom_toTopOf="@+id/profile_image"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

更新:

我已经更详细地检查了 LogCat 并发现一些行说Choreographer(abc): Skipped xx frames! The application may be doing too much work on its main thread.所以我再次检查了我的代码,我没有任何可能导致太多工作的东西,但我也检查了资源,尤其是可绘制文件并将 2 个文件(png)减少到更小大小,从1MB到大约80KB,那warnings已经消失了,颜色变化的行为也发生了一些变化。

当我之前按下返回按钮并返回应用程序时,深灰色选项卡 + 按钮变为绿色。现在,当我按下返回按钮后立即返回应用程序时,选项卡颜色保持灰色,只有片段中的按钮变为绿色。当我退出应用程序并以足够快的速度再次返回时,它会保持这种状态,但如果我等待 2 秒然后重新进入应用程序,选项卡颜色也会从灰色变为绿色。

这太奇怪了,我不知道出了什么问题。

标签: javaandroidandroid-layoutandroid-fragments

解决方案


我自己已经解决了这个问题。

TabLayout一步一步地再次使用 XML,现在它可以工作了。我没有分析我做了什么不同,但这是有效的 XML TabLayout

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".InsideActivity">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent">

<ImageView
    android:id="@+id/logo"
    android:layout_width="match_parent"
    android:layout_height="74dp"
    android:contentDescription="Title"
    android:padding="8dp"
    app:srcCompat="@drawable/logo" />

<com.google.android.material.tabs.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="65dp"
    android:background="@color/darkg"
    app:tabSelectedTextColor="@android:color/white"
    app:tabTextColor="@android:color/black">

    <com.google.android.material.tabs.TabItem
        android:id="@+id/tabItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Watchlist" />

    <com.google.android.material.tabs.TabItem
        android:id="@+id/tabItem2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ExBook" />
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>

<com.fre.book.NonSwipeableViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

推荐阅读