Android无法更改Overriden TextView控件中的属性



我创建了一个扩展文本视图的控件。不幸的是,当我尝试从我的活动中呼叫任何函数,例如setText或setTextsize时,什么也不会发生。

我通过创建一个接受标准文本视图然后使用以下代码创建控件的构造函数来解决主题问题:

    TextView l = (TextView) this.findViewById(R.id.dlgStarup_lblHeading);   
    myTextView lblHeading = new myTextView(l, _theme, _CONTROLTYPE.SUBTITLE);
    myTextView.setText("Any text can go here"); //this doesn't work

,但无法在控件创建后无法设置该控件的属性,这会导致真正的问题

这是我的myTextview类

public class myTextView extends TextView
{
    public enum _CONTROLTYPE
    {
        TITLE,SUBTITLE,MESSAGE,LABEL,LARGE_LABEL,ICON;
    }
    public myTextView(TextView _t, myTheme _theme, _CONTROLTYPE _controltype)
    {
        super(_t.getContext());
        this.setTheme(_t, _theme, _controltype);
    }
    public myTextView(Context _context, AttributeSet _attrs, int _defStyle) 
    {
        super(_context, _attrs, _defStyle);
        // TODO Auto-generated constructor stub
    }
    public myTextView(Context _context, AttributeSet _attrs) 
    {
        super(_context, _attrs);
        // TODO Auto-generated constructor stub
    }
    private void setTheme(TextView _control, theme _theme, _CONTROLTYPE _controltype)
    {
        _control.setTextColor(_theme.colours.titleText);
        _control.setTextSize(_theme.fonts.getPointSize(myTheme._POINTSIZES.TITLE));
    }
           //this didn't work
    public void setThemeOld(theme _theme, _CONTROLTYPE _controltype)
    {
        //I tried calling both this.setText.... and super.setText..., neither changed the apperance of my control
        this.setTextColor(_theme.colours.titleText);
        this.setTextSize(_theme.fonts.getPointSize(myTheme._POINTSIZES.TITLE));
        //I also tried putting this here - it had no effect
        this.invalidate()
    }
}

(编辑)根据要求,完整的文本视图启动的代码:

protected Dialog_Startup(Context _context, boolean _cancelable, OnCancelListener _cancelListener, theme _theme) 
{
    super(_context, _cancelable, _cancelListener);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.setContentView(R.layout.dialog_startup);
    TextView l = (TextView) this.findViewById(R.id.dlgStarup_lblHeading);       
    myTextView lblHeading = new myTextView(l, _theme, _CONTROLTYPE.SUBTITLE);
    l = (TextView) this.findViewById(R.id.dlgStarup_lblProgress);
    myTextView lblMessage = new myTextView(l,_theme, _CONTROLTYPE.LABEL);
    lblMessage.setText("THIS ISN'T WORKING"); //this isn't workig
    Intent i = new Intent(_context, DataService.class);
    //_context.startService(i);
}

我认为(直到您发布代码)是您从自定义文本视图创建新对象而不是在XML布局中创建新对象的问题,请尝试此代码可能会帮助您

TextView l = (TextView) this.findViewById(R.id.dlgStarup_lblHeading);   
myTextView lblHeading = new myTextView(l, _theme, _CONTROLTYPE.SUBTITLE);
l.setText("Any text can go here"); 

最新更新