在RelativeLayout中使用ImageView重复显示图片

32

我想在RelativeLayout中使用ImageView来重复图片。是否可以使用位图XML并使用平铺模式"repeat"RelativeLayout,因为我在互联网上看到的所有东西都涉及LinearLayout

欢迎任何帮助或提示。

4个回答

83
Drawable文件夹中创建一个名为background.xml的XML文件。
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/imagename"
android:tileMode="repeat" />

在你的相对布局中添加上面的XML代码作为背景

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:background="@drawable/background" />

13
如果您将此代码放入ImageView中,则设置android:scaleType="fitXY"是必要的。 - Hugo Logmans

9
当然可以,您可以将它用作RelativeLayout的背景。我在我的项目中也使用了它。
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
        android:background="@drawable/top_header_bg_repeat"
        android:gravity="center">

top_header_bg_repeat.xml (in drawable folder)
----------------------------------------------

<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/top_header"
    android:tileMode="repeat"
    android:dither="true"/>

Set Bitmap in ImageView
----------------------   

<ImageView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/top_header_bg_repeat"
        android:scaleType="fitXY"/> 

 <ImageView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/top_header_bg_repeat"/> 

2
是的,这是可能的。在XML中将您的重复图像定义为Drawable(位图),并使用android:tileMode="repeat"作为RelativeLayout的背景。

1
在所有请求中存在一个小问题,如果您的图像很大并且比您的布局尺寸小,图像将不会调整大小,只会在X轴上重复。 要在Y轴上重复,您只能通过编程实现。
我是这样解决我的问题的(设置重力填充)。
可绘制背景用于图像。
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/table_cells_bg_img"
        android:tileMode="repeat" android:gravity="fill" />

并且在布局上

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/table_cell_height"
        android:scaleType="fitXY"
        android:src="@drawable/table_cells_bg"/>
<TextView
        android:id="@+id/txtActivityTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/avatarImage"
        android:text="TextView"
        android:textColor="@color/white"
        android:textSize="@dimen/font_size_middle" />
</RelativeLayout>

table_cells_bg_img是一张宽度为1像素,高度为1像素的图片。

在布局等方面,现在您的图片就像背景一样,并且会根据父布局的任何大小进行调整。

尝试一下,也许有帮助。在我的情况下,我需要平铺的背景图像来填满表格单元格的完整大小,并通过X坐标重复(就像我可以在iOS上做的那样)。所以我按照我描述的方式解决了这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接