问题" @ ID" VS" @ + ID"在定位于相对布局 [英] issue with "@id" vs "@+id" in positioning in Relative Layout

查看:136
本文介绍了问题" @ ID" VS" @ + ID"在定位于相对布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有相关的布局略有问题,我不知道为什么。通常情况下,当你试图相对于其他意见,可以使用@id,但它似乎并没有定位在所有。只有当我使用的值@ + ID将它去正确。在下面的例子中,我有在水平方向上,我想用percentage_id被定位在imageviews,但更接近最后一间TextView的4次。

Good day, i have a slight issue with relative layouts and i don't know why. Normally when you trying to position relative to other views, you use "@id" but it doesn't seem to position at all. only when i use the value "@+id" would it go correctly. in the example below, i have 4 views in a horizontal orientation i want the TextView with "percentage_id" to be positioned in between the imageviews but closer to the last one.

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/sales_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:text="£0.00"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#AADDEE"
        android:textSize="18sp"
        android:textStyle="bold" >
    </TextView>

    <ImageView
        android:id="@+id/arrow_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_toRightOf="@id/sales_id"
        android:baselineAlignBottom="true"
        android:src="@drawable/rightarrow" >
    </ImageView>

    <TextView
        android:id="@+id/percentage_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/imagearrow_id"
        android:layout_toRightOf="@id/arrow_id"
        android:text="0.00%"
        android:textColor="#606090"
        android:textSize="18sp" >
    </TextView>

    <ImageView
        android:id="@+id/imagearrow_id"
        android:layout_width="20dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="2dp"
        android:layout_weight="0"
        android:gravity="right"
        android:padding="5dp"
        android:src="@drawable/rightarrow" >
    </ImageView>

</RelativeLayout>

现在,无论我做什么,它只是默认位置在相对布局,只有当我使用@ + ID,而不是@id它到正确的位置。我知道,有时它可以给错误,如果认为我尝试引用尚未声明还没有,但即使我把TextView的最后,我仍然无法得到它的地方,我想,直到我用@ +编号。

Now no matter what i do, it just goes to the default position in the relative layout and only when i use "@+id" instead of "@id" does it go to the correct position. I know sometimes it can give error if the view am trying to reference has not been declared yet but even if i place the textview last, i still can't get it where i want until i use "@+id".

这是一个新的东西,作品也具有相对布局?因为我不知道为什么它不工作与@id。我是罚款这种方式?任何人都遇到了同样的问题?在网上看到使用@ + ID为定位一些教程了。谢谢

is this is a new thing that works also with Relative Layout? because i have no idea why its not working with "@id". am i fine this way? anyone encountered the same issue? see some tutorials on the net use "@+id" for positioning too. Thank you

推荐答案

虽然你的可以的参考后来在层次结构中声明的看法,XML文档还是按顺序解析。第一次,你指的是特定的ID,你的有无的以preFIX用 @ + ID 。实际上,你可以preFIX每引用一个ID与 @ + ID 这样就可以了 - 其实,这是如果你设计你的界面会发生什么图形编辑器。为了安全起见,一切都是pfixed与 @ + ID $ P $。该 + 只需告诉它生成 R.java一个ID 当且仅当它尚未确定。如果你试图去定义它不止一次,这只是看到了它已经被定义,并继续正常进行。

Although you can refer to views declared later in the hierarchy, the XML document is still parsed sequentially. The first time that you refer to a particular ID, you have to prefix it with @+id. In actuality, you can prefix every reference to an ID with @+id and everything will work -- in fact, this is what happens if you design your interface with the graphical editor. Just to be safe, everything is prefixed with @+id. The + simply tells it to generate an ID in R.java if and only if it has not already been defined. If you try to define it more than once, it just sees that it's already been defined and continues on normally.

在你的XML,你从你的 percentage_id TextView的引用 imagearrow_id 。然而,在这一点上,<​​code> imagearrow_id 尚未确定。对于这种情况,你可以简单地preFIX的 layout_toLeftOf = @ +您的TextView的ID / imagearrow_id ,然后下面,定义ImageView的( imagearrow_id ),你会的没有的需要 + 机器人:ID 属性。

In your XML, you reference imagearrow_id from your percentage_id TextView. However, at this point the imagearrow_id has not yet been defined. For this scenario, you could simply prefix the layout_toLeftOf=@+id/imagearrow_id in your TextView, and then below, when defining your ImageView (imagearrow_id), you would not need the + in the android:id attribute.

这篇关于问题&QUOT; @ ID&QUOT; VS&QUOT; @ + ID&QUOT;在定位于相对布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆