Repeat Image in with ImageView in RelativeLayout

2019-01-31 12:48发布

I want to repeat the image with ImageView with in RelativeLayout. Can it be possible to use the Bitmap xml and use tilemode "repeat" with RelativeLayout, because what I have seen things on Internet they all dealing with LinearLayout.

Any help or tip will be warmly welcomed.

4条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-31 13:13

Yes of Course, You can use it with relative layout. Use it as background of your RelativeLayout. I also used it in my project.

  <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"/> 
查看更多
虎瘦雄心在
3楼-- · 2019-01-31 13:17

In all requests exists one small problem if you image will be great and smaller what size your layout, image will not resize, only repeat by X. For repeat by Y you can do it only programmly.

I solve my problem like this (set gravity fill)

drawable bg for image

<?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" />

and on layout

<?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>

And table_cells_bg_img it's image with width=1px and height = 1px

and etc of layout, so now your image it's like background and it's will resized for any size of parent layout

Try, maybe help. To be cleared in my case i need tiled background image to full size of tablecell repeated by X coordinate (like i can do it on iOS). So i solve it like i describe.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-31 13:23

Create an XML file named background in Drawable folder

background.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" />

In your Relative Layout add the above XML as background

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background" />
查看更多
我命由我不由天
5楼-- · 2019-01-31 13:28

Yes, its possible. Define your repeating image as Drawable (bitmap) with android:tileMode="repeat" in XML and use it as background of your RelativeLayout.

查看更多
登录 后发表回答