如何在ListView的每个单元格中设置阴影?
我有一个Android ListView的自定义单元格。这个自定义单元格是一个内部有一些视图的相对布局。每个单元格之间都有一个空格,所以我想在单元格的底部添加一个阴影。如何在ListView的每个单元格中设置阴影?
我一直在搜索,但找不到任何东西?我想实现类似于此:
谢谢!
可以在下面两种方式来完成:
- 9补丁图像 - 点击link理解9补丁
-
使用图层列表 - 创建包含“RES /绘制”新的文件:
<?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中或等)。
这正是我正在寻找的!非常感谢! – Andres 2013-05-01 16:27:19
谢谢....完美答案 – nitin 2013-11-15 16:58:50
酷,第二作品 – antongorodezkiy 2013-11-16 13:54:03
最简单的方法肯定是将阴影构建为9补丁。大概是这样的一个例子是:
这是比它需要的是更大的方式,作为9补丁,但我想让它如的缘故较大。
我没有测试你确切的情况还没有,但是这是如何添加一个透明分隔的列表视图:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView
android:id="@+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@android:color/transparent"
android:dividerHeight="4dip"/>
</LinearLayout>
这是如果你想添加阴影线,你需要什么编程。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/transparent" />
<stroke
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:width="match_content"
android:color="@color/black" />
<size android:height="1dp" />
</shape>
笔画的颜色可能不会显示为黑色,因为它会呈现在alpha图层的顶部。此外,它目前绘制矩形的四边,而不仅仅是底边。 (我将不得不对这两个问题做进一步的研究)。
看到这个tutorial如果你需要帮助搞清楚如何将它们连接在一起。
我将此layerlist在你的列表项布局的背景下,选择的颜色被禁用....
<?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>
创建从峰下了一个layerlist矩形,并设置你的视图自己的主要背景颜色在它的xml中。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!--shadow to bottom and right-->
<!--by pushing offset from top and left-->
<!--then set foreground-color in android:background in xml-->
<item
android:left="2dp"
android:right="0dp"
android:top="2dp"
android:bottom="0dp">
<shape
android:shape="rectangle">
<solid android:color="@android:color/darker_gray" />
</shape>
</item>
</layer-list>
in your cell.xml:
<RelativeLayout
android:layout_width="match_content"
android:layout_height="wrap_content"
android:background="@drawable/shadow">
<RelativeLayout
android:id="@+id/cell_stuff"
android:layout_width="match_content"
android:layout_height="match_content"
android:background="@android:color/holo_orange_light">
<!--cell stuff-->
</RelativeLayout>
</RelativeLayout>
或所有功能于一身的
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!--shadow to bottom and right-->
<!--by pushing offset from top and left-->
<item
android:left="2dp"
android:right="0dp"
android:top="2dp"
android:bottom="0dp">
<shape
android:shape="rectangle">
<solid android:color="@android:color/darker_gray" />
</shape>
</item>
<!--foreground-color to cover over non-shadow-->
<!--need to also offset in opposite direction-->
<item
android:left="0dp"
android:right="2dp"
android:top="0dp"
android:bottom="2dp">
<shape
android:shape="rectangle">
<solid android:color="@android:color/holo_orange_light"/>
</shape>
</item>
</layer-list>
in your cell.xml:
<RelativeLayout
android:layout_width="match_content"
android:layout_height="wrap_content"
android:background="@drawable/shadow_allinone">
<!--cell stuff-->
</RelativeLayout>
我们一直做这样的事情有艺术品。对于程序员来说,最简单的方法是制作一些你想要的尺寸,包括颜色,白色面板和阴影。然后把它放在那里。或者,您可以将每件作品 - 彩条,白色面板和阴影作为单独的艺术作品,并将它们拼成一个xml。 – HalR 2013-05-01 03:00:55
谢谢你的答案! @StephanBranczyk我问的是如何创建行的影子效果。在我附上的图片中,您可以在每行的底部看到有一个小阴影效果... – Andres 2013-05-01 03:32:27