【发布时间】:2014-09-03 14:07:33
【问题描述】:
我尝试在我的主 ScrollView 的滚动事件期间在我的操作栏上创建一个淡入淡出 Alpha:
我试过这个:
//Active Overlay for ActionBar before set my content in the activity
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
//create a new Drawable for the AB background
final ColorDrawable newColor = new ColorDrawable(getResources().getColor(R.color.primaryColor));
newColor.setAlpha(0);
getActionBar().setBackgroundDrawable(newColor);
//create a OnScrollListener to get ScrollY position
final ScrollView mainScrollView = (ScrollView) findViewById(R.id.place_detail_scrollView);
mainScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
final int scrollY = mainScrollView.getScrollY();
final int boundY = 300;
final float ratio = (float) (scrollY/boundY) ;
newColor.setAlpha((int)(ratio*255));
}
});
理论上,我的操作栏会在 ScrollView 的位置 0 到 300 之间淡化其 alpha。不幸的是,我的 AB 每次都还是透明的……有什么想法吗?
【问题讨论】:
标签: android android-actionbar alpha onscrolllistener