Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 AS3-DataGrid中的右对齐未按预期工作_Actionscript 3_Datagrid_Alignment_Right Align - Fatal编程技术网

Actionscript 3 AS3-DataGrid中的右对齐未按预期工作

Actionscript 3 AS3-DataGrid中的右对齐未按预期工作,actionscript-3,datagrid,alignment,right-align,Actionscript 3,Datagrid,Alignment,Right Align,对,我有一个带有两列的DataGrid。在左列中,我希望文本向右对齐:更具体地说,如果列太小而无法显示整个文本,我希望它包含最右边的内容 我通过创建一个扩展CellRenderer的自定义类,并将DataGrid的CellRenderer属性设置为该类,成功地将其对齐,如中所述。然而,这并不完全符合我的要求。它确实将文本向右对齐,但是如果列太小,无法容纳所有文本,它仍然只显示最左侧的部分 有人知道我如何让它显示文本的最右边部分吗 正如您所知,这是在FlashDevelop中的纯AS3 AIR应用

对,我有一个带有两列的
DataGrid
。在左列中,我希望文本向右对齐:更具体地说,如果列太小而无法显示整个文本,我希望它包含最右边的内容

我通过创建一个扩展
CellRenderer
的自定义类,并将
DataGrid
CellRenderer
属性设置为该类,成功地将其对齐,如中所述。然而,这并不完全符合我的要求。它确实将文本向右对齐,但是如果列太小,无法容纳所有文本,它仍然只显示最左侧的部分

有人知道我如何让它显示文本的最右边部分吗


正如您所知,这是在FlashDevelop中的纯AS3 AIR应用程序中,我的
CellRenderer
类与我在上面链接的文章中的类相同(即
RightAlignCell
)。

这是我的
CellRenderer
的原始代码:

package  
{
    import fl.controls.listClasses.CellRenderer;
    import fl.controls.listClasses.ICellRenderer;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;

    public class RightAlignCell extends CellRenderer implements ICellRenderer
    {
        private var tf:TextFormat;

        public function RightAlignCell() 
        {
            tf = new TextFormat();
            tf.align = TextFormatAlign.RIGHT;
        }

        override protected function drawLayout():void
        {
            textField.setTextFormat(tf);
            super.drawLayout();
        }

    }

}
但是,更改
textField.x
的值没有效果。我想这是因为我在重写版本的末尾调用了
super.drawLayout()
,这显然将值改回了默认值。如果我一开始就调用它,那么更改
x
有效(我这里使用-50作为一个明显的例子):

但是,如果我尝试将值设置为
this.width-textField.width
,正如Gio建议的那样,它只会将
x
增加5个像素。 我再次发现问题是,
super.drawLayout()
。它似乎正在调整
文本字段的宽度,以适应列内的大小,这就是我的计算不起作用的原因。我试图将其从函数中删除,但这是一个大混乱,因为它处理了许多列正确显示所需的代码。因此,我想让
textField
自动调整其大小至适当的宽度,即使
super.drawLayout()
使用
autoSize
属性调整其大小:

        override protected function drawLayout():void
        {
            super.drawLayout();
            textField.setTextFormat(tf);
            textField.autoSize = TextFieldAutoSize.LEFT;
            textField.x = this.width - textField.width;
        }
现在它成功了,尽管有一些小问题。但是这些都是最小的,并且通过将定义文本格式和自动调整大小的行移动到
CellRenderer
的构造函数(我意识到我本来应该这么做)来修复。最后,我的最后一堂功能课是:

package  
{
    import fl.controls.listClasses.CellRenderer;
    import fl.controls.listClasses.ICellRenderer;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;
    import flash.text.TextFieldAutoSize;

    public class RightAlignCell extends CellRenderer implements ICellRenderer
    {
        private var tf:TextFormat;

        public function RightAlignCell() 
        {
            tf = new TextFormat();
            tf.align = TextFormatAlign.RIGHT;
            textField.setTextFormat(tf);
            textField.autoSize = TextFieldAutoSize.LEFT;
        }

        override protected function drawLayout():void
        {
            super.drawLayout();
            textField.x = this.width - textField.width - 5;
        }

    }

}
将5个像素减去
textField.x
只是为了使文本看起来更好而做的一点定位,而不是触摸列的边框


虽然解决方案很简单,但很抱歉发了这么长的帖子,但我决定对此进行深入解释。基本上,我刚刚提出了上面的类,它起作用了。

您是否尝试更改了
textField
x
属性?只是为了清楚地表明它按预期工作。右对齐并不意味着“显示最右边的内容”。@Gio:是的,很遗憾,这似乎没有任何效果@我想你是对的,但我的意思是它没有按照我的预期工作。在像Excel这样的程序中,右对齐可以做到这一点,所以我认为Flash的
DataGrid
也是如此。无论如何,我修正了标题以反映这一点。我不在我的计算机自动取款机旁,当我到达那里时,我会尝试一下。我认为设置正确的对齐方式,并将textField的
x
属性设置为
this.width-textField.width
可以满足您的要求。@Gio:这对我不起作用,但请尝试一下。如果它能工作,我的代码一定有点奇怪。编辑:啊哈,找到问题了。似乎
textField
的宽度会发生变化,以适应列的内部,这意味着查找两者之间的差异将导致+5。我从my
CellRenderer
中覆盖的
drawLayout
函数中删除了
super.drawLayout()
,它似乎消除了特定的问题,但会出现更多问题。如果将
autoSize
设置为
TextFieldAutoSize.RIGHT
则无需
setTextFormat
package  
{
    import fl.controls.listClasses.CellRenderer;
    import fl.controls.listClasses.ICellRenderer;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;
    import flash.text.TextFieldAutoSize;

    public class RightAlignCell extends CellRenderer implements ICellRenderer
    {
        private var tf:TextFormat;

        public function RightAlignCell() 
        {
            tf = new TextFormat();
            tf.align = TextFormatAlign.RIGHT;
            textField.setTextFormat(tf);
            textField.autoSize = TextFieldAutoSize.LEFT;
        }

        override protected function drawLayout():void
        {
            super.drawLayout();
            textField.x = this.width - textField.width - 5;
        }

    }

}