我如何使用这个自定义drawable类?

时间:2014-10-09 11:53:05

标签: java android drawable

我对Android编程很新,所以我不确定这个。

我试图将png设置为背景但是当我在布局xml文件中使用android:background="@drawable/bkgrnd"时图像会拉伸

我在这里找到了这个自定义类http://www.anddev.org/viewtopic.php?p=27178#27178,但我不确定如何在我的代码中使用它。 我已将代码复制到自己的类文件中,并且不存在任何错误。

如何使用此类将背景设置为上面的drawable?

注意:我已经放弃了这种方法,并且采用了更为简单的方法。下面的答案确实提供了线索以及其他一些stackoverflow问题。答案如下。

3 个答案:

答案 0 :(得分:1)

请试试这个会对你有所帮助。

android:src="@drawable/bkgrnd"

答案 1 :(得分:0)

在XML中,声明像这样的ImageView

 <ImageView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:background="@drawable/bkgrnd"
            android:scaleType="fitXY"
            android:layout_alignParentRight="true"
            />

答案 2 :(得分:0)

使用scale drawables可以实现结果

bkgrnd.xml(可绘制的xml文件)

<?xml version="1.0" encoding="utf-8"?>`
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/bkgrnd"
android:scaleGravity="clip_horizontal"
android:scaleHeight="100%"
android:scaleWidth="100%"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

layout.xml

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bkgrnd" />