【问题标题】:Effect of muliple @+id vs single @+id in compilation of same layout file多个@+id vs 单个@+id 编译同一布局文件的效果
【发布时间】:2016-12-19 00:04:47
【问题描述】:

第一种方式:

<Button
    android:id="@+id/btnLogin"
    style="@style/btnStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btnFacebook"
    android:layout_alignRight="@+id/btnFacebook"
    android:layout_below="@id/etPassword"
    android:layout_centerInParent="true"
    android:onClick="goHome"
    android:text="Login"/>

<Button
    android:id="@+id/btnFacebook"
    style="@style/btnFacebook"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/textOR"
    android:layout_marginTop="10dp"
    android:onClick="facebookSignUp"
    android:text="     Signup with Facebook     "/>


<Button
    android:id="@+id/btnGooglePlus"
    style="@style/btnGoogle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btnFacebook"
    android:layout_alignRight="@+id/btnFacebook"
    android:layout_below="@id/btnFacebook"/>

第二种方式:

<Button
    android:id="@+id/btnLogin"
    style="@style/btnStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btnFacebook"
    android:layout_alignRight="@id/btnFacebook"
    android:layout_below="@id/etPassword"
    android:layout_centerInParent="true"
    android:onClick="goHome"
    android:text="Login"/>

<Button
    android:id="@id/btnFacebook"
    style="@style/btnFacebook"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/textOR"
    android:layout_marginTop="10dp"
    android:onClick="facebookSignUp"
    android:text="     Signup with Facebook     "/>


<Button
    android:id="@+id/btnGooglePlus"
    style="@style/btnGoogle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/btnFacebook"
    android:layout_alignRight="@id/btnFacebook"
    android:layout_below="@id/btnFacebook"/>

第三条路:

<Button
    android:id="@+id/btnLogin"
    style="@style/btnStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/btnFacebook"
    android:layout_alignRight="@id/btnFacebook"
    android:layout_below="@id/etPassword"
    android:layout_centerInParent="true"
    android:onClick="goHome"
    android:text="Login"/>

<Button
    android:id="@+id/btnFacebook"
    style="@style/btnFacebook"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/textOR"
    android:layout_marginTop="10dp"
    android:onClick="facebookSignUp"
    android:text="     Signup with Facebook     "/>


<Button
    android:id="@+id/btnGooglePlus"
    style="@style/btnGoogle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/btnFacebook"
    android:layout_alignRight="@id/btnFacebook"
    android:layout_below="@id/btnFacebook"/>

两个问题:

  1. 三种初始化方式的输出

btnFacebook

是一样的。怎么样?

  1. 由于输出相同,表示 XML Parser 编译结果相同。那么资源的初始化究竟是如何完成的,它们是如何附加到 ViewTree 的呢?

感谢任何带有解释的官方文档。 提前致谢:)

【问题讨论】:

    标签: android android-layout xml-parsing android-viewgroup android-identifiers


    【解决方案1】:

    from here

    顺序不会有任何区别。

    好像视图相同,但第三种方式不起作用,因为 btnFacebook 没有在 btnLogin 之前声明。所以会显示编译错误。 第三种方式在稍作更改后将起作用,请参阅 that 链接。 更多info

    【讨论】:

    • 是的,你是对的,第三种方式给出了编译错误。我已经浏览了那个链接,但我不明白第一种方式是如何工作的。因为当我在像 R.id 这样的 java 文件中做某事时。自动完成建议给了我不止一个 >btnFacebook 建议,这意味着有不止一个参考被创建为 btnFacebook。它将如何在运行时编译和解析。它不会占用更多不必要的内存吗?提前致谢:)
    • 我在 android studio 2.1 中检查了你的第一种方式 xml,但看不到多个 btnFacebook。
    • 即使您在不同的布局中使用相同的 ID,它也可能不会显示多个选项,但 ID 应该在当前视图内。参考:-stackoverflow.com/questions/17825356/…
    • imgur.com/uNNTzS3 这就是我要说的。一个 id 指向 layout_alignLeft 顶部的一个,另一个指向 android:id 按钮中的一个。在这里我只测试了 2 @+id 来生成图像给你截图
    • 您可以检查您的 R.java 文件,在该文件中您只能看到一个可用于 btnFacebook 的 int 值。所以,似乎只生成了一个id,并没有不必要的内存占用。
    猜你喜欢
    • 2018-08-04
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 2012-03-26
    • 2015-09-24
    • 1970-01-01
    相关资源
    最近更新 更多