Android 向上滚动时使工具栏透明,向下滚动时使其可见
我只想让android.support.v7.widget.Toolbar在向下滚动回收视图时透明,在向上滚动时清晰可见 这是我的密码Android 向上滚动时使工具栏透明,向下滚动时使其可见,android,android-toolbar,android-collapsingtoolbarlayout,android-appbarlayout,Android,Android Toolbar,Android Collapsingtoolbarlayout,Android Appbarlayout,我只想让android.support.v7.widget.Toolbar在向下滚动回收视图时透明,在向上滚动时清晰可见 这是我的密码 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/co
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorlayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbarlayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar1"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#ae43ff"
/>
<ImageView
android:id="@+id/imageview1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@drawable/unnamed"
android:scaleType="center"
app:layout_scrollFlags="scroll"
/>
<!--app:layout_scrollFlags="scroll|enterAlways"-->
<!--app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7"-->
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="#6fffc6">
</android.support.v7.widget.RecyclerView>
我尝试了很多不同的方法,但都没有达到我想要的效果
我不想要android.support.design.widget.collakingToolbarLayout中的效果
我想要这个中所示的效果,我也面临着类似的问题,我用这种方式破解了这个问题
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/detail_backdrop_height"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="206dip"
android:background="@android:color/transparent"
app:expandedTitleTextAppearance="@android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="@android:color/transparent"
app:expandedTitleMarginStart="48dp"
app:titleEnabled="false">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/backdrop"
android:src="@drawable/ss"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/backdrop"
android:layout_marginBottom="20dp"
android:orientation="vertical">
<TextView
android:id="@+id/txtSomeTextDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Display1"
android:textSize="16sp"
android:textColor="#ffffff" />
<TextView
android:id="@+id/txtSomeOtherText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Display 2"
android:textSize="12sp"
android:textColor="#ffffff" />
</LinearLayout>
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
我的一个项目取得了预期的效果。如果你需要更多的帮助,请告诉我。由于我仍在完善代码,代码可以做得更好,如果有人有建议,请随时发表评论。您可以通过将布局行为设置到工具栏来实现。写了一篇文章,可以帮助您了解它是如何工作的,以及如何利用CoordinatorLayout.Behavior
的强大功能。这里还有一个
基本上,它计算AppBarLayout
和当前AppBarLayout
底部的总滚动范围之间的比率。该比率用于将alpha设置为工具栏
背景
差不多完美了,谢谢。除了setAlpha
方法得到一个浮点。因此,您应该将代码更改为*.setAlpha(intColorCode/256f)
我使用了您的代码,但它不是透明背景。我想要像snapchat在discover Screen中所做的那样注意SDK中有一个bug,当您从代码中更改背景的alpha时,您也会更改所有使用该颜色作为背景的视图的alpha。所以我建议使用ColorUtils.setAlphaComponent(color,alpha)。它将生成新的颜色与阿尔法每次,但我们避免了错误。
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
int intColorCode=0;
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
intColorCode=-(verticalOffset);
if(intColorCode>255)
intColorCode=255;
toolbar.getBackground().setAlpha(intColorCode);
toolbar.setAlpha(intColorCode);
}
});