两段代码告诉你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" >

    <com.cctvjiatao.baiduwapper.ui.BottomLayout
        android:id="@+id/myBottomLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/image_titlebar_background" />

    <android.support.v4.view.ViewPager
        android:id="@+id/myViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/myBottomLayout" />

</RelativeLayout>
代码二:

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

    <com.cctvjiatao.baiduwapper.ui.BottomLayout
        android:id="@+id/myBottomLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/image_titlebar_background" />

    <android.support.v4.view.ViewPager
        android:id="@+id/myViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/myBottomLayout" />

</RelativeLayout>

两段代码的区别就在于  android:layout_above="@+id/myBottomLayout"

以上布局文件加载到类中的时候,代码一正常,代码二报错。

分析:

1、Android中的组件需要用一个int类型的值来表示,这个值也就是组件标签中的id属性值。id属性只能接受资源类型的值,也就是必须以@开头的值,例如,@id/abc、@+id/xyz等。

2、如果在@后面使用“+”,表示当修改完某个布局文件并保存后,系统会自动在R.java文件中生成相应的int类型变量。变量名就是“/”后面的值,例如,@+id/xyz会在R.java文件中生成int xyz = value,其中value是一个十六进制的数。如果xyz在R.java中已经存在同名的变量,就不再生成新的变量,而该组件会使用这个已存在的变量的值。

3、也就是说,如果使用@+id/name形式,当R.java中存在名为name变量时,则该组件会使用该变量的值作为标识。如果不存在该变量,则添加一个新的变量,并为该变量赋相应的值(不会重复)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值