Android ImageView构造函数用法
有几个构造函数可用于定义ImageView。 比如说Android ImageView构造函数用法,android,imageview,Android,Imageview,有几个构造函数可用于定义ImageView。 比如说 1) public ImageView (Context context) 2) public ImageView (Context context, AttributeSet attrs) 3) public ImageView (Context context, AttributeSet attrs, int defStyle)** 我在使用第二类和第三类构造函数时感到困惑。 基本上我不知道通过什么来代替属性集。 请提供一个编码示例。
1) public ImageView (Context context)
2) public ImageView (Context context, AttributeSet attrs)
3) public ImageView (Context context, AttributeSet attrs, int defStyle)**
我在使用第二类和第三类构造函数时感到困惑。
基本上我不知道通过什么来代替属性集。
请提供一个编码示例。这些构造函数在
视图
文档中定义。以下是以下参数的说明:
参数
- Context视图运行的上下文,通过它可以访问当前主题、资源等
- 属性膨胀视图的XML标记的属性
- DefStyle应用于此视图的默认样式。如果为0,则不会应用任何样式(主题中包含的样式除外)。今年五月 或者是属性资源,其值将从中检索 当前主题或显式样式资源
null
来代替AttributeSet
在对属性集
进行编码方面,这里有一些我用于自定义文本视图
类的代码:
public EKTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// ...
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LocalTextView);
determineAttrs(context, a);
}
// ...
}
private void determineAttrs(Context c, TypedArray a) {
String font = a.getString(R.styleable.fontName);
if (font != null)
mTypeface = Typeface.createFromAsset(c.getAssets(), "fonts/" + font);
mCaps = a.getBoolean(R.styleable.allCaps, false);
}
如您所见,一旦从属性中获得
TypedArray
,您就可以使用来收集每个属性。您可能需要查看的其他代码是或。创建imageView的方法,imageView与上下文
ImageView image= new ImageView(context);
在这里,当你想设置像高度,宽度,重力等你需要设置的值
image.set****();
根据需要使用的属性的数量,没有setXXX()方法
2.使用属性集
您可以在单独的xml文件中的res/values文件夹中定义一组属性,如高度、宽度等,然后将xml文件传递给getXml()
在这里,您还可以在xml中定义自定义属性。您可以使用AttributeSet类示例提供的方法访问
getAttributeFloatValue(int index, float defaultValue)
//返回'index'处属性的浮点值。在创建自定义视图时,最后两个构造函数非常有用。您可以看到exampleAttributeSet的意思是,如果我们想向CustomView添加任何新属性,例如ImageView视图,默认情况下有
Android:id,Android:src,…
,现在我想添加一个属性myimageview:secondsrc
,然后为AttributeSet创建一个xml。我想现在是clearPragnani了,请您提供属性所在的XML的详细信息。
getAttributeFloatValue(int index, float defaultValue)