Android工具栏背景问题(闪烁和可绘制问题)

Android工具栏背景问题(闪烁和可绘制问题),android,android-drawable,android-toolbar,Android,Android Drawable,Android Toolbar,我有一个应用程序,它的AppCompat工具栏实现如下 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:layout_weight="0

我有一个应用程序,它的AppCompat工具栏实现如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:layout_weight="0">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical">

        <LinearLayout
            android:id="@+id/toolbar_bg"
            android:animateLayoutChanges="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="0dp"
            android:gravity="right|center_vertical"
            android:padding="0dp">

            <TextView
                android:animateLayoutChanges="true"
                android:id="@+id/action_bar_restaurant_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/restaurant_label_action_bar_margin_start"
                android:layout_weight="1"
                android:ellipsize="end"
                android:gravity="left"
                android:singleLine="true"
                android:textColor="@color/white"
                android:shadowDy="1"
                android:shadowDx="1"
                android:shadowRadius="1"
                android:shadowColor="@color/black"
                android:textSize="@dimen/restaurant_label_action_bar_font_size"
                />

            <LinearLayout
                android:focusable="true"
                android:layout_weight="1"
                android:animateLayoutChanges="true"
                android:id="@+id/search_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginRight="14dp"
                android:gravity="center_vertical"
                android:background="@drawable/search_bg_focused"
                android:orientation="horizontal">

                <AutoCompleteTextView
                    android:id="@+id/search_view"
                    android:layout_width="0dp"
                    android:background="@color/transparent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:hint="@string/search"
                    android:imeOptions="actionSearch"
                    android:inputType="text"
                    android:maxLines="1"
                    android:paddingLeft="2dp"
                    android:paddingBottom="4dp"
                    android:shadowColor="@color/black"
                    android:shadowDx="1"
                    android:shadowDy="1"
                    android:shadowRadius="1"
                    android:singleLine="true"
                    android:textColor="#FFFFFFFF"
                    android:textColorHint="#FFFFFFFF"
                    style="@style/Base.Widget.AppCompat.EditText"
                    />

                <ImageView
                    android:id="@+id/search_clear"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:paddingLeft="16dp"
                    android:paddingRight="0dp"
                    android:src="@drawable/abc_ic_clear_mtrl_alpha" />
            </LinearLayout>
            .
            .
            . (more content here but it has not effect)
            . (I already tried removing them and had the same behavior)
            .
            .
        </LinearLayout>


    </android.support.v7.widget.Toolbar>
</LinearLayout>
考虑到background drawable/actionbar_background.xml实际上如下所示:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#CC0000"></solid>
</shape>
我从工具栏的这种外观开始:

当我在工具栏中单击任何可以修改工具栏内容的内容(例如显示搜索字段)时,我通常会得到以下结果:

但只要我调用上面的方法,它就会重置背景可绘制,并且工具栏的整个背景可绘制再次可见

我做错了什么

这只发生在我从ActionBar迁移到工具栏时

Kitkat 4.4.x、棒棒糖5.0和5.1上的行为相同

有人建议:默认情况下,工具栏不会重新绘制其背景


但是我怎么能强迫它,而不是在每次更改后手动执行,并看到这种闪烁的效果呢?

我刚刚在两个月后想出了一个解决方案

只需将用于设置背景的方法更改为将其设置为“颜色”,而不是“背景可绘制”,即可解决此问题:

public void updateActionBarBackground(int scrollY, int initialScroll)
{
    float alphaRatio = ((1f * scrollY) - (1f * initialScroll)) / (1f * initialScroll);

    int alpha = Math.min((int) Math.round(alphaRatio * MAX_ALPHA_VALUE), MAX_ALPHA_VALUE);

    Drawable bg = getResources().getDrawable(R.drawable.actionbar_background);
    Drawable bgStatus = getResources().getDrawable(R.drawable.statusbar_background);
    bg.setAlpha(alpha);
    bgStatus.setAlpha(alpha);
    toolbar.setBackgroundColor(getResources().getColor(R.color.medium_red));
    toolbar.getBackground().setAlpha(alpha);

    tintManager.setStatusBarTintDrawable(bg);
    tintManager.setNavigationBarAlpha(0);
}
public void updateActionBarBackground(int scrollY, int initialScroll)
{
    float alphaRatio = ((1f * scrollY) - (1f * initialScroll)) / (1f * initialScroll);

    int alpha = Math.min((int) Math.round(alphaRatio * MAX_ALPHA_VALUE), MAX_ALPHA_VALUE);

    Drawable bg = getResources().getDrawable(R.drawable.actionbar_background);
    Drawable bgStatus = getResources().getDrawable(R.drawable.statusbar_background);
    bg.setAlpha(alpha);
    bgStatus.setAlpha(alpha);
    toolbar.setBackgroundColor(getResources().getColor(R.color.medium_red));
    toolbar.getBackground().setAlpha(alpha);

    tintManager.setStatusBarTintDrawable(bg);
    tintManager.setNavigationBarAlpha(0);
}