Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中重复背景图像_Android_Xml_Image_Background_Repeat - Fatal编程技术网

如何在Android中重复背景图像

如何在Android中重复背景图像,android,xml,image,background,repeat,Android,Xml,Image,Background,Repeat,我正在尝试为布局的背景创建一个xml。代码如下: <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid a

我正在尝试为布局的背景创建一个xml。代码如下:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
            <solid android:color="@android:color/transparent" />
            <stroke android:width="2dp" android:color="@color/text_color" />
        </shape>
    </item>
    <item android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp"><bitmap android:src="@drawable/paper_texture" android:tileMode="repeat" />
    </item>
</layer-list>


我正在扩展布局,并将此xml设置为它的背景。问题是,当我用xml编写布局时,它工作得很好,但当我以编程方式膨胀它时,背景图像不会重复,而是适合整个布局。

当使用布局设置背景时,布局会膨胀到重复模式,因为它知道要填充的适当大小。当您以编程方式充气时,充气机不知道要充气到什么高度,因此您将无法获得重复模式。当您将膨胀视图放置为背景时,它别无选择,只能拉伸。您可以尝试在充气时设置parentView

LinearLayout ll = (LinearLayout) findViewById(R.id.customWrapper);
View view;
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
view = inflater.inflate(R.layout.custom, ll);
这样,充气机就知道需要充气的高度/宽度

希望这有帮助