【问题标题】:adding shadow on the bottom of List item of Listview在 Listview 的列表项底部添加阴影
【发布时间】:2014-04-08 12:06:26
【问题描述】:

我想在我的 ListView 项的底部添加阴影! 我尝试了 paddingEdgeLenght 但没有发生任何变化! 我添加了圆形边缘,但我无法在项目下方添加阴影

这是我的列表项 xml 文件

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fadingEdgeLength="5sp" >

    <ImageView 
        android:id="@+id/site_image"
        android:layout_height="50dp"
        android:layout_width="65dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"/>

    <TextView 
        android:id="@+id/site_name"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@id/site_image"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:textSize="20sp"/>

    <TextView 
        android:id="@+id/site_description"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@id/site_name"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/site_image"
        android:textSize="15sp"/>


</RelativeLayout>

这是我的 ListView xml 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/near_sites"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:fadingEdgeLength="5sp"
        android:dividerHeight="10sp"
        android:divider="@android:color/transparent" />

</RelativeLayout>

有什么帮助吗?

【问题讨论】:

标签: android listview android-listview shadow


【解决方案1】:

list_view_item 中,在RelativeLayout 之后添加一个虚拟 视图,并将它们全部包装在垂直LinearLayout 上,如下所示:

<LinearLayout
         android:orientation="vertical">

    <RelativeLayout>
        ...
        ...
    </RelativeLayout>

    <View
        android:id="@+id/shadow"
        android:layout_width="fill_parent"
        android:layout_height="3dp" <!--or your needed height value-->
        android:background="@drawable/shadow_drawable">     
    </View>

</LinearLayout>

并在drawable文件夹中添加新的shadow_drawable.xml并把它放在这里

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android=”http://schemas.android.com/apk/res/android”>    
    <gradient
        android:startColor="@color/#000000" <!--black-->
        android:endColor="@color/#FFFFFF" <!--white-->
        android:angle="90" <!-- angle of the gradient-->
        >
    </gradient>
</shape>

【讨论】:

  • 在 Material Design 中,分隔线比您建议的解决方案暗。因此,最好使用深灰色作为形状中的起始颜色。像这样:android:startColor="@android:color/darker_gray"
【解决方案2】:

你必须在列表视图中添加页脚视图,

如何添加footerview检查波纹管代码,

private View footerView;
footerView = ((LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listview_footer, null, false);
ListView.addFooterView(footerView);

所以,在页脚视图中你可以做什么。

【讨论】:

  • (R.layout.listview_footer) 包含什么?
  • 嗨,这是 xml 布局。
【解决方案3】:

您可以制作带有阴影效果的边框的 9-patch 图像。

在“res/drawable”中新建一个名为“border.xml”的drawable资源文件,编写如下代码-

    <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item >
        <shape
            android:shape="rectangle">
            <gradient android:startColor="#ddd" android:endColor="#555" android:type="linear" />
            <corners android:radius="0dp"/>
        </shape>
    </item>
    <item android:right=".5dp" android:left=".5dp" android:bottom="2dp" android:top=".5dp">
        <shape
            android:shape="rectangle">
            <solid android:color="@android:color/white"/>
            <corners android:radius="0dp"/>
        </shape>
    </item>
</layer-list>

只需将android:background="@drawable/border" 放在要应用边框的位置即可。

您可以更改android:rightandroid:leftandroid:topandroid:bottom 以赋予边框大小。您也可以在android:shape 中进行更改以获得更多效果。

希望对你有所帮助!

【讨论】:

  • 你好,你试过了吗?
猜你喜欢
  • 2020-04-21
  • 1970-01-01
  • 1970-01-01
  • 2019-11-06
  • 2017-04-14
  • 1970-01-01
  • 2021-02-24
  • 1970-01-01
  • 2022-01-25
相关资源
最近更新 更多