![](/img/trans.png)
[英]How to make the AppBarLayout have a transparent background like Gmail?
[英]How to make AppBarLayout background transparent
我正在尝试制作类似于Play商店中的搜索片段:
我的AppBarLayout
有一个CardView
,背景设置为透明:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:elevation="0dp">
<!-- CardView -->
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!-- Main content -->
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
但正如您所看到的, CardView
背后有一个坚实的背景,以便隐藏它背后的内容:
如何使AppBarLayout
的背景透明,以便后面的内容可见?
同样的问题发生在我身上,此时它与AppBarLayout
的透明度AppBarLayout
。 但是当淡入时, AppBarLayout
会将AppBarLayout
的内容推NestedScrollView
自身下方。
你可以解决这个问题
NestedScrollView
起来,后面AppBarLayout
和 NestedScrollView
添加一个空格元素 通过将此添加到onCreate()
可以轻松实现步骤1
mNestedScroll.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mNestedScroll.getViewTreeObserver().removeOnGlobalLayoutListener(this);
final int appBarHeight = mAppBar.getHeight();
mNestedScroll.setTranslationY(-appBarHeight);
mNestedScroll.getLayoutParams().height = mNestedScroll.getHeight() + appBarHeight;
}
});
对于第2步 ,你需要与appBar的高度间隔您添加一个空的观点NestedScrollView
。 注意:我在我的方法中使用了RecyclerView
而不是NestedScrollView
,所以我不得不将带有appBarHeight的空ViewHolder添加到我的RecyclerView中。 所以这段代码没有经过测试,但它应该为你做的诀窍:
<View
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
尝试将CardView放入具有match_parent
维度的FrameLayout中,以便CardView具有围绕它的空间。 我不是百分之百地确信它会起作用,但值得尝试。
我尝试了很多代码来给我或多或少的东西。 这是一个替代解决方案,但您需要将AppBarLayout更改为一个简单的视图...看看:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/annonce.main.coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="RtlHardcoded">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- here you put your search bar, can be any view !-->
<android.support.v7.widget.CardView
android:id="@+id/searchbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="4dp"
app:cardUseCompatPadding="true"
app:layout_behavior="[package].RecyclerSearchBehavior">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="Pesquise aqui"
android:minHeight="58dp"/>
</android.support.v7.widget.CardView>
注意:这是在CoordinatorLayout中。 创建一个行为,该行为是您使用的搜索栏的类型:
public class RecyclerSearchBehavior extends CoordinatorLayout.Behavior<CardView> {
//Initial height and location
private int mViewHeight;
private int[] mViewStartLocation;
public RecyclerSearchBehavior(Context context, AttributeSet attrs) {
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, final CardView child, View dependency) {
//the first function called. The initial variables settings.
//getting height
mViewHeight = child.getHeight();
//getting location on screen
mViewStartLocation = new int[2];
child.getLocationOnScreen(mViewStartLocation);
//if the dependecy is your recycler
if (dependency instanceof RecyclerView)
//add scroll listener
((RecyclerView) dependency).addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
//here the magic happens
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//animate function
animate(child, dy);
}
});
return dependency instanceof RecyclerView;
}
private void animate(CardView child, int dy) {
//the initial position of your search bar is 0, because it is on top, so...
if (child.getY() <= 0)
//dy is an integer that shows you if user scrolls up or down
if (dy >= 0) {
//move to top is a negative value, so *-1
//if that is < than height, scrools up
if ((child.getY() * -1) <= (mViewHeight))
child.setTranslationY(child.getY() - dy * 0.1f);
} else {
//if the position of search bar is < than zero, scrolls down
if (child.getY() < 0)
child.setTranslationY(child.getY() - dy * 0.1f);
//else, put the view on 0y
else child.setY(0);
}
//else, put the view on 0y again. this function is called after any user
//touches... so everything happens fast and with numbers different than
//1
else child.setY(0);
}
}
并做了。 您的“搜索栏”就在这里。
这里唯一的问题是,你需要在Recycler上的第一个项目中添加填充,或者在第一个项目中添加透明视图。
另一种选择是向NestedScrollView
(或任何使用appbar_scrolling_view_behavior
视图)添加负上边距,并为其直接子项添加相同的正上边距(或填充):
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_marginTop="-100dp" >
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="100dp" >
...
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null" >
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed" >
...
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
请注意,为了使这种方法起作用, AppBarLayout
应该放在布局文件中的NestedScrollView
下面。 如果NestedScrollView
淡化边缘产生视觉伪像,请将android:overScrollMode="never"
到其参数中。 因为它在使用elevation
时也要小心,当在错误的位置使用时(比如,当应用于整个AppBarLayout
),也可能产生视觉伪像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.