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

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样