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