我尝试了一个带有以下 xml 的简单 Hello World 应用程序:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="text1"
/>
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="text2"
/>
</LinearLayout>
我的两个文本视图都具有相同的 ID。它编译得很好,运行得很好,同时渲染了两个 TextView,当我执行 findViewByid() 时,第一个被找到,并且我的所有函数调用(如 setText)都应用于它。理想情况下,AAPT 应该抓住这一点,但显然没有。除非程序员依赖 id,否则它不会严重破坏某些东西。所以这就像是在说:如果你傻到写出这样的代码,那么你应该崩溃。
AAPT 不会太在意它。对它来说,这就像一个简单的视图扩展,程序员没有提供明确的 id。