每当在我的应用程序中的某个活动上面打开一个对话框片段或者它经历一个屏幕尺寸转换(例如多窗口调整大小)时,工具栏就会出现奇怪的视觉故障。
我通过代码手动设置工具栏的颜色,这似乎是问题的一部分:
getSupportActionBar().setTitle("Scout: Team " + scoutData.getTeamNumber()); //TODO match number, Qualification
getSupportActionBar().setSubtitle("Qualification Match " + scoutData.getMatchNumber());
int toolbarColor = ((ColorDrawable) findViewById(R.id.toolbar).getBackground()).getColor();
int statusBarColor;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
statusBarColor = getWindow().getStatusBarColor();
} else {
statusBarColor = getResources().getColor(R.color.primary_dark);
}
TransitionUtils.toolbarAndStatusBarTransition(toolbarColor, statusBarColor,
getResources().getColor(scoutData.getAllianceColor().getColorPrimary()),
getResources().getColor(scoutData.getAllianceColor().getColorPrimaryDark()), this);
TransitionUtils中的方法:
public static void toolbarAndStatusBarTransition(final int toolbarColor, final int statusBarColor, final int toolbarToColor, final int statusBarToColor, final AppCompatActivity activity) {
ValueAnimator anim = ValueAnimator.ofFloat(0, 1);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// Use animation position to blend colors.
float position = animation.getAnimatedFraction();
// Apply blended color to the status bar.
int blended = blendColors(statusBarColor, statusBarToColor, position);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.getWindow().setStatusBarColor(blended);
}
// Apply blended color to the ActionBar.
blended = blendColors(toolbarColor, toolbarToColor, position);
ColorDrawable background = new ColorDrawable(blended);
activity.getSupportActionBar().setBackgroundDrawable(background);
if (activity instanceof ScoutingFlowActivity) { //we don't want a random null
activity.findViewById(R.id.app_bar_layout).setBackground(background);
activity.findViewById(R.id.tab_layout).setBackground(background);
}
}
});
anim.setDuration(350).start();
//if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { //tint in Overview
//ActivityManager.TaskDescription tDesc = new ActivityManager.TaskDescription(null, null, toolbarToColor);
//activity.setTaskDescription(tDesc);
//}
}
这是我的活动布局:
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="top|center_horizontal"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="?attr/colorPrimary"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabPaddingEnd="30dp"
app:tabPaddingStart="30dp" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:scaleType="center"
android:src="@drawable/ic_send_white_24dp"
android:visibility="invisible"
app:fabSize="normal"
app:backgroundTint="@color/accent"
app:elevation="6dp"
app:pressedTranslationZ="12dp" />
</android.support.design.widget.CoordinatorLayout>
这是迄今为止我遇到过的最奇怪的故障。它实际上并没有伤害任何东西,但是非常令人恼火。
答案 0 :(得分:0)
通过更改
修复// Apply blended color to the ActionBar.
blended = blendColors(toolbarColor, toolbarToColor, position);
ColorDrawable background = new ColorDrawable(blended);
activity.getSupportActionBar().setBackgroundDrawable(background);
if (activity instanceof ScoutingFlowActivity) { //we don't want a random null
activity.findViewById(R.id.app_bar_layout).setBackground(background);
activity.findViewById(R.id.tab_layout).setBackground(background);
}
到
// Apply blended color to the ActionBar.
blended = blendColors(toolbarColor, toolbarToColor, position);
activity.findViewById(R.id.toolbar).setBackgroundColor(blended);
if (activity.findViewById(R.id.tab_layout) != null) { //we don't want a random NPE
activity.findViewById(R.id.tab_layout).setBackgroundColor(blended);
}
if语句中的更改无关紧要 - 使用似乎已解决问题的setBackgroundColor()
代替setBackground()
。