2015-04-02 57 views
0

我与程序兼容性的toolbard一个应用程序实现的:的Android工具栏背景文档版本(闪烁和绘制对象发行)

<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当用户滚动与滚动视图以下代码:

公共无效updateActionBarBackground(INT scrollY,INT initialScroll) { 浮子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); 
} 

鉴于背景 “绘制/ 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> 

我开始使用工具栏的这一下ñ感觉: enter image description here

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

但是,只要我调用上述方法(它重置背景可绘制),并且工具栏的整个背景(可绘制)再次变为可见。

我在这里做错了什么?

这只是开始发生的时候,我从动作条迁移到工具栏上的奇巧4.4.x到

相同的行为,棒棒糖5.0和5.1

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

但我怎么能强制它,而不需要每次更改后手动执行并看到这种闪烁效果?

回答

0

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

只是改变用来设置背景的方法有它设置的颜色,而不是背景绘制对象似乎是解决这个问题:

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