Using ImageView Constructor

Several constructors are available to define an ImageView. For example

1) public ImageView (Context context)
2) public ImageView (Context context, AttributeSet attrs)
3) public ImageView (Context context, AttributeSet attrs, int defStyle)** 

      

I am confused about using the 2nd and 3rd type constructor. basically I don't know what to pass in instead of the AttributeSet. Please provide sample code.

+3


source to share


2 answers


Ways to create imageView, ImageView with context

ImageView image= new ImageView(context);

      

Here when you want to set values ​​like height, width, etc., you need to set

image.set****();

      

depending on the number of attributes you need to use the setXXX () methods,



2.Using a set of attributes you can define a set of attributes like height, width, etc. in the res / values ​​folder in a separate XML file, pass the xml file to getXml ()

XmlPullParser parser = resources.getXml(yourxmlfilewithattribues);
 AttributeSet attributes = Xml.asAttributeSet(parser);
ImageView image=new ImageView(context,attributes);

      

You can also define your custom attributes in your xml here too. and you can access them using the methods provided by the example AttributeSet class

getAttributeFloatValue(int index, float defaultValue)

      

// Return the value of the float attribute at 'index'

+1


source


These constructors are defined in the documentation View

. Below is a description of the parameters from View(Context, AttributeSet, int)

:

Parameters

  • context The context in which the view is launched, through which it can access the current topic, resources, etc.
  • attrs, attributes of an XML tag that inflates the view.
  • defStyle is the default style for this view. If 0, the style will not be applied (other than what is included in the theme). This can either be an attribute resource whose value will be fetched from the current theme, or an explicit style resource.

It's worth noting that you can pass null

instead AttributeSet

if you don't have the attributes to pass.



In terms of coding, AttributeSet

here's a bit of code that I'm using for a custom class TextView

, I have:

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);
}

      

As you can see, once you get an attribute TypedArray

from attributes, you can simply use its various methods to collect each of the attributes. Another code you can look at is - >View(Context, AttributeSet, int)

or Resources.obtainStyledAttributes(AttributeSet, int[], int, int)

.

+2


source







All Articles