Android 如何在ListView的每个单元格中设置阴影?
我有一个Android ListView的自定义单元格。此自定义单元是一个相对布局,其中包含一些视图。每个单元格之间都有一个空格,所以我想在单元格底部添加一个阴影 我一直在谷歌搜索,但什么都没找到?我想实现类似的目标:Android 如何在ListView的每个单元格中设置阴影?,android,xml,android-ui,Android,Xml,Android Ui,我有一个Android ListView的自定义单元格。此自定义单元是一个相对布局,其中包含一些视图。每个单元格之间都有一个空格,所以我想在单元格底部添加一个阴影 我一直在谷歌搜索,但什么都没找到?我想实现类似的目标: 谢谢 最简单的方法肯定是将阴影构建成一个9面片。类似的例子如下: 作为一个9-patch,这比它需要的要大得多,但我想把它放大一些,例如为了方便起见。我还没有测试您的具体场景,但这就是您将如何向列表视图添加透明分隔符的方法: <LinearLayout xmlns:
谢谢 最简单的方法肯定是将阴影构建成一个9面片。类似的例子如下:
作为一个9-patch,这比它需要的要大得多,但我想把它放大一些,例如为了方便起见。我还没有测试您的具体场景,但这就是您将如何向列表视图添加透明分隔符的方法:
<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层的顶部渲染的。另外,这将当前绘制矩形的四条边,而不仅仅是底部。(我必须对这两个问题做进一步的研究)
如果您需要了解如何将其连接在一起的帮助,请参阅此部分。可以通过以下两种方式完成:
<?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等)。我将此图层列表应用为列表项布局中的背景,选定的颜色将被禁用
<?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>
创建一个从下方突出的图层列表矩形,并在其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>
在cell.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-->
<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>
在cell.xml中:
我们总是用艺术品做这些事情。对于程序员来说,最简单的方法是制作一些接近您想要的大小的东西,包括颜色、白色面板和阴影。然后把它放在那里。或者,您可以将每一块——色带、白色面板和阴影作为单独的艺术块,并将它们拼成xml。谢谢大家的回答@Stephanbraczyk我要问的是如何创建行的阴影效果。在我附上的图片中,你可以在每一行的底部看到一个小阴影效果…这正是我想要的!!非常感谢你!