在横向布局中定义 id



我在两个xml文件(默认和横向)中定义活动布局。在这两种情况下,我使用相同的视图,只改变它们的位置和格式。
这是定义视图 id 属性的正确方法:

android:id="@+id/example_text_view"  //using @+id in both xml files

或:

android:id="@+id/example_text_view"  //default orientation xml
android:id="@id/example_text_view"   //landscape orientation xml

两者都似乎可以正常工作,除非我从 land xml 文件中删除 id 声明。

不同之处在于+将在 R 中创建一个新 id.java而另一个不会。因此,在引用 id 时,请勿包含 + 。查看此链接以获取有关此内容的更多信息

你应该

使用

android:id="@+id/example_text_view" 

基本上,您始终使用属性android:id您应该使用@+id因为您正在为视图定义一个 id。

正如扎伊德所说,当你引用另一个视图时,你应该使用@id。例如:

android:layout_below="@id/example_text_view"

这样,您就是说此视图应位于ID为"example_text_view"的视图下方

最新更新