当ScrollView或RecyclerView Z索引较小时,在底视图上显示阴影高程-Android材质设计
我曾经在工具栏视图下方强制添加阴影,以获得更好的向后支持,如下所示:当ScrollView或RecyclerView Z索引较小时,在底视图上显示阴影高程-Android材质设计,android,android-layout,material-design,Android,Android Layout,Material Design,我曾经在工具栏视图下方强制添加阴影,以获得更好的向后支持,如下所示: <View android:id="@+id/toolbar_shadow" android:layout_width="match_parent" android:layout_height="4dp" android:layout_below="@+id/toolbar" android:backg
<View
android:id="@+id/toolbar_shadow"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_below="@+id/toolbar"
android:background="@drawable/shadow_elevation" />
@可绘制/阴影高度
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:endColor="#12000000"
android:startColor="@android:color/transparent" />
</shape>
现在我需要做同样的效果,但在底部是这样的
<--Toolbar-->
<--toolbar shadow-->
<--Scroll View-->
<--bottom shadow-->
<--Bottom Layout-->
问题是,我不想让底部阴影始终可见,我只想在scrollview位于底部布局的“下方”时显示“底部阴影”,即Z索引。
换句话说,当scrollview底部点击底部布局顶部时,我需要显示底部阴影
这是底部视图上没有阴影的布局:
我一直在考虑对代码执行此操作,检查视图的Y索引,如果它们相同,这意味着底部布局需要比Scrollview具有更高的仰角/平移Z,但我不确定这是否是最佳选项,我认为可能有一种方法可以正确设置布局
有什么想法吗?我找到了办法!我完全忘了把它放在这里 对于那些需要帮助的人来说,到目前为止,它一直运作良好
public void setBottomLayoutElevation(View scrollView, View bottomView) {
if (scrollView instanceof NestedScrollView) {
NestedScrollView nestedScrollView = (NestedScrollView) scrollView;
nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
int max = nestedScrollView.getChildAt(0).getHeight() - nestedScrollView.getHeight();
if (v.canScrollVertically(1)) {
int abs = Math.abs((scrollY * 100 / max) - 100);
ViewCompat.setElevation(bottomView, Math.min(abs, 40));
} else {
ViewCompat.setElevation(bottomView, 0);
}
}
});
}
if (scrollView instanceof RecyclerView) {
((RecyclerView) scrollView).addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
ViewCompat.setElevation(bottomView, recyclerView.canScrollVertically(1) ? 30 : 0);
}
});
}
}