在android中如何在视图上方添加阴影
我有一个作为页脚标题发送到服务器的视图。它只是一个视图,你可以把它想象成一个按钮、文本视图或布局(我对任何事情都很开放)。这是xml在android中如何在视图上方添加阴影,android,android-layout,material-design,Android,Android Layout,Material Design,我有一个作为页脚标题发送到服务器的视图。它只是一个视图,你可以把它想象成一个按钮、文本视图或布局(我对任何事情都很开放)。这是xml <RelativeLayout> <ScrollView >… </ScrollView> //match parent width and height <MyBottomView/> // align parent bottom </RelativeLayout> …//匹配父级宽度
<RelativeLayout>
<ScrollView >… </ScrollView> //match parent width and height
<MyBottomView/> // align parent bottom
</RelativeLayout>
…//匹配父级宽度和高度
//将父项底部对齐
如您所见,ScrollView确实在MyBottomView下方滚动。我想给MyBottomView添加一个顶部阴影,使其看起来更像材质设计。我该怎么做呢?这里有一些解决这个问题的方法-选择你最好的:
- 关于StackOverFlow,在2012年10月22日的邮件中,您会看到:
res/drawable
。包含:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape
android:shape="rectangle">
<solid android:color="@android:color/darker_gray" />
<corners android:radius="5dp"/>
</shape>
</item>
<item android:right="1dp" android:left="1dp" android:bottom="2dp">
<shape
android:shape="rectangle">
<solid android:color="@android:color/white"/>
<corners android:radius="5dp"/>
</shape>
</item>
</layer-list>
所以我找到了这个:
android:background="@android:drawable/dialog_holo_light_frame"
看起来是这样的:
!![在此处输入图像描述][1]
[1] :
如果您正在安装clean Material Design effect,请阅读以下文档:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingBottom="10dp"
android:rotation="180">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rotation="180"
app:cardElevation="8dp">
<!--Card view content-->
</android.support.v7.widget.CardView>
</RelativeLayout>
这仍然有一个问题-这需要在父布局上设置paddingBottom,这意味着很明显,布局上方的任何可滚动同级都不会在其下方
因此,即使在如今的立面和轮廓提供程序时代,最好还是添加半透明视图(如果需要在视图的一侧(例如顶部)有阴影,可以在其前面添加另一个视图,并使用渐变阴影作为其背景
这是梯度文件top\u shadow\u gradient.xml
,您必须将其存储在drawable
文件夹中:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#40000000" android:endColor="#30ffffff" android:angle="90"/>
</shape>
下面是如何使用它的示例布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="8dp"
android:background="@drawable/top_shadow_gradient" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical"
android:paddingTop="8dp">
<!-- Put your content here-->
</LinearLayout>
</LinearLayout>
重要提示:根布局必须是透明的(android:background=“@android:color/transparent”
),并且您的“内容”布局需要有白色背景(android:background=“#ffffff”
)
这就是结果:
唯一对我有效的解决方案可能会重复,但我在求阴影时总是会得到一条陌生的白线。将endcolor设置为完全透明的黑色#0000会去除白色。然而,此策略虽然简单,但不会完全正确地创建阴影状照明。请参阅如何使用绘画的指针。setXferemode()角半径呢?另一个问题是,它会将CardView中的任何内容倒置!是的,这需要让孩子也旋转180度。我知道,很糟糕。这不是真正的解决方案。完全是。这就是我在回答中开始说的:)尽管它很黑,但不幸这是可用的最佳解决方案。添加形状会产生可怕的白色条纹,并且远不如此解决方案优雅
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingBottom="10dp"
android:rotation="180">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rotation="180"
app:cardElevation="8dp">
<!--Card view content-->
</android.support.v7.widget.CardView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#40000000" android:endColor="#30ffffff" android:angle="90"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="8dp"
android:background="@drawable/top_shadow_gradient" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical"
android:paddingTop="8dp">
<!-- Put your content here-->
</LinearLayout>
</LinearLayout>