一个简单的解决方案可能对您的整个应用都有帮助,那就是覆盖“onRender”事件。使用 Ext.util.TextMetrics 计算文本宽度:
(这里是radiogroup的一个例子)
Ext.define('NG.override.form.field.Radio', {
override: 'Ext.form.field.Radio',
autoBoxLabelWidth: false,
onRender: function () {
var me = this,
boxlabelWidth;
me.callParent(arguments);
me.renderActiveError();
if (me.autoBoxLabelWidth) {
me.tm = new Ext.util.TextMetrics(me.getEl());
boxlabelWidth = me.tm.getWidth(me.boxLabel) + Ext.Number.from(me.autoBoxLabelWidth, 0);
me.setWidth(boxlabelWidth);
}
}
});
然后将属性 'autoBoxLabelWidth' 设置为 'true' 或要添加的像素数:
xtype: 'radiogroup',
fieldLabel: 'Two Columns',
columns: 2,
vertical: true,
items: [{
boxLabel: 'Item 1',
name: 'rb',
autoBoxLabelWidth: true,
inputValue: '1'
}, {
boxLabel: 'Item 2',
name: 'rb',
autoBoxLabelWidth: 15,
inputValue: '2',
checked: true
}]