myc_100 2013-09-27 07:04 采纳率: 10%
浏览 3545
已结题

在不同的 layout xml 中的 id 必须不同吗?

在 Main.xml 中有两个 layout xml 文件,有一个按钮名称是android:id="@+id/btnClose" 在 About.xml 中也有一个按钮名称是 android:id="@+id/btnClose"

这样行吗?
Main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="3dip"
    android:layout_marginTop="3dip"
    android:background="#DCDCDC" >

     <Button
        android:id="@+id/btnClose"
        style="@style/myTextAppearance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="@string/exit" />
</RelativeLayout>

About.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"   
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="3dip"
    android:paddingLeft="7dip"
    android:background="@drawable/border_ui"
    android:orientation="vertical" >

     <Button
        android:id="@+id/btnClose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        style="@style/myTextAppearance"
        android:text="@string/myreturn" />
</LinearLayout>
  • 写回答

2条回答

  • 羊男 2013-09-29 03:29
    关注

    当然可以啊。即使是同一个布局里也可以使用相同的ID。
    具有相同ID的View,如果它们隶属于不同布局,程序运行时这些同ID View将隶属于不同的对象,findViewById会依据调用它的对象来区分这些View;如果它们在同一个布局里,使用findViewById方法通常会得到最前面的那个(整个布局会组织成一个树形结构,findViewById方法从根View开始往下查找,查到第一个就返回)。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作