Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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
Android 发出「@id";vs"@+;id";在相对布局中的定位_Android - Fatal编程技术网

Android 发出「@id";vs"@+;id";在相对布局中的定位

Android 发出「@id";vs"@+;id";在相对布局中的定位,android,Android,你好,我有一个相对布局的小问题,我不知道为什么。通常,当您尝试相对于其他视图定位时,您使用“@id”,但它似乎根本不定位。只有当我使用值“@+id”时,它才会正确运行。在下面的示例中,我有4个水平方向的视图,我希望带有“percentage_id”的TextView位于图像视图之间,但更靠近最后一个视图 <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_conten

你好,我有一个相对布局的小问题,我不知道为什么。通常,当您尝试相对于其他视图定位时,您使用“@id”,但它似乎根本不定位。只有当我使用值“@+id”时,它才会正确运行。在下面的示例中,我有4个水平方向的视图,我希望带有“percentage_id”的TextView位于图像视图之间,但更靠近最后一个视图

<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放在最后一个位置,在使用“@+id”之前,我仍然无法将其放在我想要的位置


这是一个新事物,也适用于相对布局吗?因为我不知道为什么它不使用“@id”。我这样好吗?有人遇到过同样的问题吗?请参阅网上的一些教程,也可以使用“@+id”进行定位。谢谢

@+id
是在首次声明id时使用的,您不应该在元素定位时使用它。它应该只用于
android:id
属性

另一方面,
@id
用于引用现有id

相对视图中,您不能引用当前代码中引用视图下方的视图。因此,如果您有以下布局:

LayoutRoot
    View1
    View2
    View3
View1不能或不应该使用View2和View3来定义其位置。View2应仅使用View1来定义其位置,View3可以同时使用View1和View2

本质上,只要在布局文件中的引用视图上方声明了通过它引用的视图,就可以使用
@id

因此,只需切换最后两个小部件的代码块位置,您就可以使用
@id

<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>

    <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>

    <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>

</RelativeLayout>

尽管您可以参考层次结构中稍后声明的视图,但XML文档仍然是按顺序解析的。第一次引用特定ID时,必须在其前面加上
@+ID
。实际上,您可以使用
@+ID
作为ID的每个引用的前缀,一切都会正常工作——事实上,如果您使用图形编辑器设计界面,就会发生这种情况。为了安全起见,所有内容都以
@+id
作为前缀。
+
只是告诉它在
R.java
中生成一个ID,如果并且仅当它还没有被定义。如果您多次尝试定义它,它只会看到它已经被定义并正常继续


在XML中,从
percentage\u id
TextView中引用
imagearrow\u id
。但是,此时尚未定义
imagearrow\u id
。对于这种情况,您只需在TextView中为
layout\u-toLeftOf=@+id/imagearrow\u-id
添加前缀,然后在下面定义ImageView(
imagearrow\u-id
)时,您就不需要在
android:id
属性中添加
+

尝试ConstraintLayout后,检查下面的代码片段,两个视图都包装在constraintLayout中,我只粘贴案例代码:

<android.support.v4.widget.ContentLoadingProgressBar
            android:id="@+id/progress"
            android:layout_width="150dp"
            android:layout_height="150dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintVertical_bias="0.3"
            style="?android:attr/progressBarStyleLarge"
            android:visibility="@{safeUnbox(viewmodel.isLoading) ? View.VISIBLE : View.INVISIBLE}"
            />

        <TextView
            android:id="@+id/click_load"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center_horizontal"
            android:text="@{viewmodel.text}"
            android:textSize="22sp"
            android:visibility="@{safeUnbox(viewmodel.isLoading) ? View.INVISIBLE : View.VISIBLE}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/progress"
            tools:text="ojoooihoihohoihiohhoihio" />
在编辑器中显示固定在视图窗口顶部的textview,运行时一切看起来都很好,但这仍然令人困惑,现在如果您这样写的话

 app:layout_constraintTop_toBottomOf="@+id/progress"

按预期在UI编辑器的进度下方显示textview,通过KCopock解释阅读,我发现它与案例相符。

您显示的代码,它是否是一个正常工作的变体?@Nick yeah只要我在textview android:layout\u toRightOf=“@id/arrow\u id”中写下这一行,它就会工作这很奇怪。在声明视图时使用+;当引用一个时跳过它。由于尚未声明
imagearrow\u id
one,因此无法从文本视图中引用它。尝试将TextView声明放在带有
imagearrow\u id
的ImageView之后。不完全正确
@+id
在首次声明id时使用,但如果尚未定义所引用的元素,则绝对必须将其用于定位元素。例如,
percentage\u id
引用了
imagearrow\u id
,但
imagearrow\u id
定义如下
percentage\u id
。在这种情况下,
percentage\u id
TextView中对
imagearrow\u id
的引用应该以
@+id
作为前缀。我知道。我的意思是,首先,您不应该使用尚未声明的元素来定义定位。如果元素没有声明,并且您绝对不能在引用视图之前声明它,那么您应该使用@+id。不过,就个人而言,您认为应该在引用元素之前声明它们。不过,在某些情况下,您不能这样做。例如,如果子视图的Z顺序很重要,那么您必须无序地定义它们。我在评论中说,“如果元素没有声明,并且您绝对不能在引用视图之前声明它,那么您应该使用@+id:-@raghasood,非常感谢!!很抱歉,只能接受一个答案。@kcopock您是否有关于这句话的参考资料“如果您多次尝试定义它,它只会看到它已经被定义并正常继续。”
 app:layout_constraintTop_toBottomOf="@+id/progress"