【问题标题】:android listview item style?android listview 项目样式?
【发布时间】:2013-08-01 15:14:54
【问题描述】:

在我发布这个帖子之前,我已经用谷歌搜索了(如何设置列表视图项目的样式),我找不到一个很好的例子来展示如何设置列表视图项目的样式(普通、触摸、长按等)背景颜色,我也想做喜欢这个带有边框半径和框阴影的 VK 列表视图,我真的需要这个帮助,其他人也在搜索这个有什么例子或者有人可以告诉我我必须在项目的 xml 选择器背景中放置什么吗?

图片一显示如何列表视图项目有边框半径和阴影

当我点击该项目时显示的图像 2

那么各位有没有办法做到这一点呢?

【问题讨论】:

    标签: android listview styles


    【解决方案1】:

    当然,最好在这里使用样式:

    <!-- res/values/styles.xml -->
    <style name="ListView" parent="@android:style/Widget.ListView">
        <item name="android:background">@color/light_grey</item>
        <item name="android:cacheColorHint">@android:color/transparent</item>
        <item name="android:divider">@android:color/transparent</item>
        <item name="android:dividerHeight">0dp</item>
        <item name="android:listSelector">@drawable/list_item_selector</item>
    </style>
    

    @color/light_grey 定义:

    <!--- res/values/colors.xml --->
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
       <color name="light_grey">#cccccc</color>
    </resources>
    

    您必须在colors.xml 中定义light_grey 颜色并在您的drawables 文件夹中创建卡片样式list_item_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">    
        <item android:drawable="@drawable/item_selected" android:state_pressed="true"/>
        <item android:drawable="@drawable/item_focused" android:state_focused="true"/>
        <item android:drawable="@drawable/item_normal"/> 
    </selector> 
    

    【讨论】:

    • selector 内,您正在使用drawable 设置背景,但我需要像#ffffff 这样设置背景颜色如何?
    • 因为您正在查看带有阴影的卡片样式,所以您必须使用 9-patch 可绘制对象,否则选择器将延伸到每个列表项的边缘,这看起来很难看.
    • @Krylez 如何将其设置为listView 此代码:style="@style/ListView" 不起作用
    【解决方案2】:

    最好的方法是在样式.xml 文件中覆盖您在 AppTheme 中的值

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:listSelector">@color/teal_100</item>
    </style>
    

    此覆盖反映了您在应用中使用的所有 ListView。

    你也可以使用 drawable 来代替颜色。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多