【问题标题】:Repeat Image in with ImageView in RelativeLayout在 RelativeLayout 中使用 ImageView 重复图像
【发布时间】:2012-08-29 01:39:16
【问题描述】:

我想用ImageViewRelativeLayout 重复图像。是否可以使用 Bitmap xml 并使用 tilemode "repeat"RelativeLayout,因为我在 Internet 上看到的东西都处理 LinearLayout

我们将热烈欢迎任何帮助或提示。

【问题讨论】:

    标签: android bitmap android-imageview android-relativelayout


    【解决方案1】:

    Drawable文件夹中创建一个名为background的XML文件

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

    在您的相对布局中添加上述 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" />
    

    【讨论】:

    • 如果你把它放在 ImageView 中,设置 android:scaleType="fitXY" 是必要的。
    【解决方案2】:

    当然可以,您可以将它与相对布局一起使用。将其用作您的 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"/> 
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        在所有请求中都存在一个小问题,如果您的图像会很大并且您的布局尺寸会变小,图像不会调整大小,只会重复 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 是宽度=1px,高度=1px的图片

        等布局,所以现在你的图像就像背景一样,它会为任何大小的父布局调整大小

        试试吧,也许有帮助。要在我的情况下清除,我需要将平铺背景图像平铺到按 X 坐标重复的完整大小的表格单元(就像我可以在 iOS 上那样)。所以我像我描述的那样解决它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-06-22
          • 2013-08-16
          • 2011-10-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多