use*_*104 8 android android-resources
这个文档总是困惑我:
对于ID值,通常应使用以下语法形式:"@ + id/name".加号+表示这是一个新的资源ID,aapt工具将在R.java类中创建一个新的资源整数(如果它尚不存在).例如:
<TextView android:id="@+id/nameTextbox"/>
Run Code Online (Sandbox Code Playgroud)
我已经编程了很长一段时间了.但是,我从来没有遇到任何我必须使用没有加号的ID声明的情况.这也是违反直觉的.ID应该是唯一的!
这有什么好的用例吗?为什么要重新使用相同的资源ID名称?
@ + id/name 创建时new id
"@ id /"当你link to existing id
用例示例1:
假设您使用XML创建了自己的资源:
<resources>
<item name="plusIcon" type="id"/>
</resources>
Run Code Online (Sandbox Code Playgroud)
现在,您可以在多个位置使用此资源,而无需使用@ + id创建新资源.说layout_one.xml:
<TextView android:id="@id/plusIcon"/>
Run Code Online (Sandbox Code Playgroud)
资源相同layout_two.xml:
<TextView android:id="@id/plusIcon"/>
Run Code Online (Sandbox Code Playgroud)
用例示例2:
Android框架提供了许多其他ID资源.如果您想在这种情况下引用Android资源ID,可以使用@android,您不需要创建自己的新资源ID
这意味着如果您已经声明了layout_one.xml类似的视图
<TextView
android:text="Sample Text"
android:id="@+id/text_view_sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
如果你有类似的textView layout_two.xml就像
<TextView
android:text="Sample Text2"
android:id="@+id/text_view_sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,只会创建一个id R.java,它将被重用到另一个xml中(以后将被调用).
在这里你可以住在(里layout_two.xml)
<TextView
android:text="Sample Text2"
android:id="@id/text_view_sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
这里Android将重用之前创建的id layout_one.xml.
你也应该阅读在Android的"@ ID /"和"@ + ID /"之间的区别和@id和@ + ID之间的区别是什么?
首先
当我们第一次在特定的 xml 文件中引用一个 id(从上到下的顺序)时,我们使用 + ,而不是当我们第一次创建一些 id 时。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/another_button"
android:layout_alignParentTop="true"
android:text="@string/button" />
<Button
android:id="@id/another_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/another_button" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
第二
当有人使用RelativeLayout或ConstraintLayout时,即一些相对父视图,他们需要使用相同的id才能定义活动或活动中的某些视图等。
第三
加号 (+) 表示这是一个必须创建的新资源名称并将其添加到我们的资源中(在 R.java 文件中)。所以每次我们使用@+id/some_id时,都会触发创建对同一视图的新资源引用,即多余的。
示例(针对第二个用例)
<RelativeLayout
android:id="@+id/final_order_activity_order_rl"
android:layout_margin="5dp"
android:background="@drawable/gradient_for_btns"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:layout_alignParentBottom="true"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_centerInParent="true"
android:layout_marginStart="8dp"
android:textStyle="bold"
android:textSize="18dp"
android:text="$2000"
android:textColor="@android:color/white"
android:maxLines="1"
android:layout_toLeftOf="@+id/final_order_activity_place_order_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/final_order_activity_total_tv" />
<Button
android:paddingStart="6dp"
android:paddingEnd="6dp"
android:layout_marginEnd="8dp"
android:text="Place Order"
android:background="@drawable/ripple_effect"
android:textColor="@color/baseColorBright"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="28dp"
android:id="@id/final_order_activity_place_order_btn"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
315 次 |
| 最近记录: |