我正在尝试将边栏图像添加到 ListView 中的某些项目。 图像应占据项目的整个高度,并在必要时垂直重复。 拉伸(stretch)图像不是一种选择。
在尝试了一些事情之后,我最终修改了找到的代码 here . 不幸的是,它似乎在 ListView 中不起作用,并且图像不会重复:
主要布局:
<?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" />
背景图片:
为整个 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>
结果截图如下:
我删除了我以前的解决方法的代码,因为它更像是一种黑客攻击,而且效果不如这个。
关于Android:ListView 项目中的平铺位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22065781/