以下是解决此问题的几种方法-选择最适合您的方法:
Android中没有显示阴影的属性。但可能的解决方法有:
- 添加一个纯色LinearLayout,然后在其上添加您的实际布局,并使底部和右侧的边距等于1或2 dp。
- 使用带有阴影的9-patch图像,并将其设置为线性布局的背景。
还有另一种解决问题的方法,即实现LayerList,用作LinearLayoout的背景。
将background_with_shadow.xml文件添加到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>
然后在你的LinearLayout中将layer-list作为背景添加。
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_with_shadow"/>
您还可以阅读:
http://odedhb.blogspot.com/2013/05/android-layout-shadow-without-9-patch.html
另外,StackOverflow上的另一篇帖子
how to set shadow to a View in android? 给出了另一个解决方案(使用两个形成阴影的视图)。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="#CC55CC">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0">
<TableRow>
<LinearLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="@string/hello" />
</LinearLayout>
<View
android:layout_width="5dp"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:background="#55000000"/>
</TableRow>
</TableLayout>
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:layout_marginLeft="5dp"
android:background="#55000000"/>
</LinearLayout>
</FrameLayout>
您还可以使用特定的Android资源可绘制形式来模拟阴影效果。请参见:
Android View shadow 或只需阅读下面的帖子:
- 我正在使用Android Studio 0.8.6,但我找不到:
android:background="@drawable/abc_menu_dropdown_panel_holo_light"
所以我找到了这个:
android:background="@android:drawable/dialog_holo_light_frame"
它看起来像这样:
![智能手机运行Android,具有运行代码示例的图片](https://istack.dev59.com/tId9L.webp)
如果您对清新的材料设计效果感兴趣,请阅读以下一些文档: