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

时间:2015-04-02 23:21:19

标签: 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">

    <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>

然后,当用户使用以下代码在滚动视图中滚动时,我以编程方式更改背景颜色和Alpha:

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);
    getSupportActionBar().setBackgroundDrawable(bg);

    // The following part is not important it's a lib for navigation and status bar
    tintManager.setStatusBarTintDrawable(bg);
    tintManager.setNavigationBarAlpha(0);
}

给出背景&#34; drawable / actionbar_background.xml&#34;实际上如下:

<?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>

我从这个工具栏的外观开始: enter image description here

当我点击工具栏中任何可修改工具栏内容的内容时(例如显示搜索字段),我经常会遇到以下情况: enter image description here

但是一旦我调用上面的方法(它重置了背景可绘制)并且工具栏的整个背景(可绘制)再次可见。

我在这里做错了什么?

这只是在我从ActionBar迁移到工具栏时才开始发生

Kitkat 4.4.x,Lollipop 5.0和5.1上的相同行为

有人建议:工具栏默认不重绘其背景可绘制

但是我怎么能强制它,而不是在每次更改后手动操作并看到这种闪烁效果?

1 个答案:

答案 0 :(得分:0)

我刚刚在2个月后想出了解决方案!!

只是更改用于设置背景的方法,使其设置颜色而不是Background Drawable似乎解决了问题:

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);
}