The constructor with Context
and AttributeSet
is used when your view is inflated from xml. You shouldn't use it to create object. You should use constructor with Context
as param.
AttributeSet
is interface and you can create instance of then and implement all method as is shown below:
AttributeSet attrs = new AttributeSet(){
@Override
public int getAttributeCount() {
return 0;
}
@Override
public String getAttributeName(int index) {
return null;
}
@Override
public String getAttributeValue(int index) {
return null;
}
@Override
public String getAttributeValue(String namespace, String name) {
return null;
}
@Override
public String getPositionDescription() {
return null;
}
@Override
public int getAttributeNameResource(int index) {
return 0;
}
@Override
public int getAttributeListValue(String namespace, String attribute, String[] options, int defaultValue) {
return 0;
}
@Override
public boolean getAttributeBooleanValue(String namespace, String attribute, boolean defaultValue) {
return false;
}
@Override
public int getAttributeResourceValue(String namespace, String attribute, int defaultValue) {
return 0;
}
@Override
public int getAttributeIntValue(String namespace, String attribute, int defaultValue) {
return 0;
}
@Override
public int getAttributeUnsignedIntValue(String namespace, String attribute, int defaultValue) {
return 0;
}
@Override
public float getAttributeFloatValue(String namespace, String attribute, float defaultValue) {
return 0;
}
@Override
public int getAttributeListValue(int index, String[] options, int defaultValue) {
return 0;
}
@Override
public boolean getAttributeBooleanValue(int index, boolean defaultValue) {
return false;
}
@Override
public int getAttributeResourceValue(int index, int defaultValue) {
return 0;
}
@Override
public int getAttributeIntValue(int index, int defaultValue) {
return 0;
}
@Override
public int getAttributeUnsignedIntValue(int index, int defaultValue) {
return 0;
}
@Override
public float getAttributeFloatValue(int index, float defaultValue) {
return 0;
}
@Override
public String getIdAttribute() {
return null;
}
@Override
public String getClassAttribute() {
return null;
}
@Override
public int getIdAttributeResourceValue(int defaultValue) {
return 0;
}
@Override
public int getStyleAttribute() {
return 0;
}
};
And use it
TextView textView = new TextView(this, attrs);
but it is not correct way.
You should use methods from your view to set properties of view.
For example to set LayoutParams is two way to do this
First by method setLayoutParams()
view.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
The second when you added your view to ViewGroup;
viewGroup.addView(yourView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
When you have or you want to add your view to for example to RelativeLayout
you should use LayoutParams
relevant for this ViewGroup. It is RelativeLayout.LayoutParams
<style="AutoDroid" parent="Droid">
,<style="DeceptaDroid" parent="Droid">
– Clarisclarisa