Java 当应用程序在反按后恢复时,布局元素背景色会发生变化
我有两个片段的活动。活动仅包含TabLayout(两个选项卡),其中这两个片段填充选项卡主体 当我第一次启动应用程序时,所有颜色都可以从Java 当应用程序在反按后恢复时,布局元素背景色会发生变化,java,android,android-layout,android-fragments,Java,Android,Android Layout,Android Fragments,我有两个片段的活动。活动仅包含TabLayout(两个选项卡),其中这两个片段填充选项卡主体 当我第一次启动应用程序时,所有颜色都可以从colors.xml正确加载。当我按下backpress按钮(在2个模拟器和真实设备中试用,API从23到28)并重新打开应用程序时,活动选项卡的color(#22222),以及片段中所有其他元素的color(#22222)都会更改为原色,在我的例子中,原色是colorAccent:@color/green。并且该绿色颜色正常加载。应用程序中的其他颜色一点也不改
colors.xml
正确加载。当我按下backpress
按钮(在2个模拟器和真实设备中试用,API从23到28)并重新打开应用程序时,活动选项卡的color(#22222)
,以及片段中所有其他元素的color(#22222)
都会更改为原色,在我的例子中,原色是colorAccent:@color/green
。并且该绿色
颜色正常加载。应用程序中的其他颜色一点也不改变,它们按照定义保持不变
我花了6个小时调试这个问题,我不知道为什么其他颜色应该保持不变
我已经尝试并添加了一个新的活动,当我点击片段中的按钮时开始,我将此活动背景色设置为与我的colors.xml
中定义的22222
相同的
此活动中的背景始终为绿色,即使在我正常启动应用程序(不恢复)后,而活动选项卡按钮为黑色
这是我从他们的家长活动开始的方式:
@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在emulator上测试它,标签颜色在那里没有改变,只有片段中的其他元素。另一方面,我使用API 28和真实设备API 28在emulator上进行了测试,tab按钮也变为绿色,fragment+中的其他元素的活动始终为绿色
我还应该提到,在我的片段中,我有recyclerview
,我有备用行
背景色设置,如下所示(它在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 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>
更新:
我更详细地检查了LogCat,发现一些行写着:Choreographer(abc):跳过了xx帧!应用程序可能在其主线程上做了太多的工作。
因此我再次检查了我的代码,我没有任何可能导致太多工作的内容,但我也检查了资源,特别是绘图和减少了2个文件(png)变小了,从1MB
到大约80KB
,警告消失了,颜色变化行为也发生了一些变化
当我之前按下“后退”按钮并返回应用程序时,深灰色的tab+按钮变为绿色。现在,当我在按下“后退”按钮后立即返回应用程序时,tab颜色保持为灰色,只有片段中的按钮变为绿色。当我离开应用程序并以足够快的速度一次又一次返回时,它会保持这种状态,但如果我等待2秒左右回到应用程序,标签颜色也会从灰色变为绿色
这太奇怪了,我不知道出了什么问题。我自己解决了这个问题
我又一次用XML一步一步地浏览了TabLayout
,现在它开始工作了。我没有分析我做的不同之处,但这是在使用XMLTabLayout
:
<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 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>
我自己解决了这个问题
我又一次用XML一步一步地浏览了TabLayout
,现在它开始工作了。我没有分析我做的不同之处,但这是在使用XMLTabLayout
:
<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 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>
你也可以发布你的xml文件吗?@RickSanchez是的,我用xml文件更新了我的问题。你也可以发布你的xml文件吗?@RickSanchez是的,我用xml文件更新了我的问题。