Android:ListView 项目中的平铺位图

标签 android listview background bitmap tile

我正在尝试将边栏图像添加到 ListView 中的某些项目。 图像应占据项目的整个高度,并在必要时垂直重复。 拉伸(stretch)图像不是一种选择。

在尝试了一些事情之后,我最终修改了找到的代码 here . 不幸的是,它似乎在 ListView 中不起作用,并且图像不会重复:

enter image description here

主要布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       />
</RelativeLayout>

ListView 项目布局:

<?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="wrap_content" >

    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/background"
        android:orientation="vertical">
    <ImageView
        android:id="@+id/listitem_sidebar"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"       
        android:src="@drawable/sidebar"
        android:visibility="invisible"/>    
    </LinearLayout>
    <TextView 
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="400dp"
        android:text="Test"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

可绘制的背景:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/sidebar"
    android:tileMode="repeat" />

背景图片:

enter image description here

为整个 ListView 全局设置侧边栏不是一个选项,因为我在生产代码中为项目使用不同的 View 类型。

编辑:格式化

最佳答案

下车,终于让它按照我想要的方式工作。我在 TextView 下面添加了一个透明 View ,以强制 ListView 测量项目的高度并进行布局传递。然后我使用不可见的 View 作为侧边栏的 anchor 。

唯一需要修改的文件是 listview_item.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="wrap_content" >

    <TextView 
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="400dp"
        android:layout_alignParentRight="true"
        android:text="Test"/>
    <View
        android:id="@+id/dummy"
        android:layout_below="@id/textview"
        android:layout_width="match_parent"
        android:layout_height="0dp"/> 

        <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/background"
        android:layout_alignParentTop="true"
        android:layout_above="@id/dummy"
        android:orientation="vertical">
    <ImageView
        android:id="@+id/listitem_sidebar"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"               
        android:src="@drawable/sidebar"
        android:visibility="invisible"/>    
    </LinearLayout>
</RelativeLayout>

结果截图如下:

enter image description here

我删除了我以前的解决方法的代码,因为它更像是一种黑客攻击,而且效果不如这个。

关于Android:ListView 项目中的平铺位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22065781/

相关文章:

android - 在不丢失 Android 数据的情况下更新数据库表

java - 如何在android的 ListView 中返回点击的阀门?

java - 如何在列表适配器上显示警报对话框

android - 一个 ListView 在 AnimatedCrossFade 中不可滚动

iphone - iPhone中如何在应用程序后台模式下调用webservice?

ios - 应用程序进入后台模式时如何处理核心数据迁移?

java - Android解析对象KSOAP

android - Kotlin Android:将位图分配给Imageview

android - 获取最小显示亮度

ios - 使用 UIAppearance 更改后更新导航栏