如何在java代码中读取View的android默认属性

时间:2015-01-19 13:08:15

标签: java android xml

我想要一个名为MyTextView的自定义TextView,我想在它的构造函数中设置一个默认的TextSize,前提是它的XML定义中没有设置TextSize。如何检测是否已在其XML定义中设置了TextSize?

public class MyTextView extends TextView{    
    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //How can i read TextSize from AttribureSet??
        //if no TextSize has been set then SetTextSize(defaultTextSize);
    } 
}

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

您可以获取

等样式属性
public class MyTextView extends TextView{    
    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
         String size = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "textSize");
    } 
}

答案 1 :(得分:0)

这里的例子:

final Resources.Theme theme = context.getTheme();
TypedArray array = theme.obtainStyledAttributes(attrs, R.styleable.YourStylable, R.attr.YourDefStyleAttr, 0);

答案 2 :(得分:0)

试试这个float size = new TextView(this).getTextSize();

相关问题