如何在RelativeLayout中强制执行循环引用?

问题描述

| 我在RelativeLayout中有两个必须互相引用的小部件。从技术上讲,这不是循环引用,因为小部件A与小部件B垂直对齐,而小部件B与小部件A水平对齐。这是我的代码(简明):
    <Button android:id=\"@+id/btnLanguageFrom\"
        android:layout_width=\"wrap_content\" 
            android:layout_height=\"wrap_content\"
            android:layout_alignParentTop=\"true\"
            android:layout_alignParentLeft=\"true\"
            android:layout_toLeftOf=\"@id/imgArrow\"
            android:text=\"English\" />

    <ImageView android:id=\"@+id/imgArrow\"
        android:layout_width=\"wrap_content\" 
            android:layout_height=\"wrap_content\"
            android:layout_marginRight=\"10dp\" 
            android:layout_alignParentTop=\"true\"
            android:layout_alignBottom=\"@id/btnLanguageFrom\"
            android:layout_centerHorizontal=\"true\"
            android:src=\"@drawable/arrow_right\" />
但是,当我建立我得到这个错误:   错误:找不到资源   匹配给定名称(位于   \'layout_toLeftOf \'具有值   \'@ id / imgArrow \')。 有趣的是,Eclipse中的“图形布局”视图可以正确显示它,并且不会抱怨循环引用。 我看不到两个小部件沿不同的尺寸(水平和垂直)相互引用的问题,因为它不会引起无限循环。有什么办法可以解决这个问题?这是我知道获得所需布局的唯一方法。 提前致谢, 巴里 附言有什么方法可以像C / C ++一样提前声明id?     

解决方法

第一次引用ID时,请使用@ +前缀,它告诉资源生成器添加ID,而不是尝试查找它。因此,请尝试:
android:layout_toLeftOf=\"@+id/imgArrow\"
    ,循环引用是您要避免的事情。但是要解决您的问题,只需切换
Button
ImageView
的定义,以便首先定义您在ѭ2refer中引用的
ImageView
。由于某些原因,不可能反过来进行。 另外,我将始终使用相同的引用类型。因此,在您的情况下:
\"@+id/imgArrow\"
    

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...