Repeat Image in with ImageView in RelativeLayout

喜夏-厌秋 提交于 2019-11-28 17:29:13

问题


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.


回答1:


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



回答2:


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:


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




回答4:


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.



来源:https://stackoverflow.com/questions/12262735/repeat-image-in-with-imageview-in-relativelayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!