我在一个应用程序栏内有三个选项卡布局。当我更改当前标签时,我也想更改应用栏的背景颜色。下面的代码不能很好地工作,因为背景颜色并非总是正确设置的。我的意思是,应该将要设置给艺术家的颜色设置为跟踪,或者在艺术家选项卡中设置专辑的背景颜色,诸如此类。当我启动该应用程序时,没有设置背景色。
我该如何解决?
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="app_bar_artist">#FFF29312</color>
<color name="app_bar_album">#FFEDEFEB</color>
<color name="app_bar_track">#FF3E86A0</color>
</resources>
main_activity.xml
<android.support.design.widget.AppBarLayout
android:id="@+id/activity_main_app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="20">
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/toolbar_layout"/>
<android.support.design.widget.TabLayout
android:id="@+id/activity_main_tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/activity_main_app_bar_layout"
app:tabGravity="fill"
app:tabMode="fixed"/>
</android.support.design.widget.AppBarLayout>
-
public class MainActivity extends AppCompatActivity {
private AppBarLayout appbar;
// others implementations here but not related to the problem.
public void setTabLayout() {
ArtistFragment artistFragment = new ArtistaFragment();
AlbumFragment albumFragment = new AlbumFragment();
TrackFragment trackFragment = new TrackFragment();
artistFragment.setAppBarLayout(appbar);
albumFragment.setAppBarLayout(appbar);
trackFragment.setAppBarLayout(appbar);
viewPagerAdapter.addFragment(artistFragment, getResources().getString(R.string.fragment_artist_title));
viewPagerAdapter.addFragment(albumFragment, getResources().getString(R.string.fragment_album_title));
viewPagerAdapter.addFragment(trackFragment, getResources().getString(R.string.fragment_track_title));
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appbar = findViewById(R.id.activity_main_app_bar_layout);
// others implementations here but not related to the problem.
setTabLayout();
}
}
-
public class ArtistFragment extends Fragment {
private AppBarLayout appBarLayout;
public ArtistFragment() {}
public void setAppBarLayout(AppBarLayout appBarLayout) {
this.appBarLayout = appBarLayout;
}
private void setBackgroundColor() {
appBarLayout.setBackgroundColor(getResources().getColor(R.color.app_bar_artist));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_artist, container, false);
setBackgroundColor();
return view;
}
}
-
public class AlbumFragment extends Fragment {
private AppBarLayout appBarLayout;
public AlbumFragment() {}
public void setAppBarLayout(AppBarLayout appBarLayout) {
this.appBarLayout = appBarLayout;
}
private void setBackgroundColor() {
appBarLayout.setBackgroundColor(getResources().getColor(R.color.app_bar_album));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_album, container, false);
setBackgroundColor();
return view;
}
}
我没有发布TrackFragment,因为它与ArtistFragment和AlbumFragment几乎相同。