【问题标题】:Android Toolbar Background Issue (Flickering and Drawable Issue)Android 工具栏背景问题(闪烁和可绘制问题)
【发布时间】:2015-04-02 23:21:19
【问题描述】:

我有一个应用程序,其 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);
}

假设背景“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 迁移到 Toolbar 时才开始发生的

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

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

但是我如何强制它,而不是在每次更改后手动执行并看到这种闪烁效果?

【问题讨论】:

    标签: android android-drawable android-toolbar


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 2013-03-26
      • 2017-01-31
      • 1970-01-01
      • 2015-02-13
      • 2011-11-25
      • 1970-01-01
      相关资源
      最近更新 更多