【发布时间】:2012-09-08 22:05:27
【问题描述】:
在我的项目中,我需要为视图设置始终不变的 ID。我的意思是在不同的应用程序构建之间保持不变的 ID。经过一番调查,我发现它可以通过使用values/public.xml 来实现,并且在该文件中声明的任何 id 都不会在未来的构建中更改。现在的问题是我无法在某些布局文件中定义视图的 id。
这是我的 layout.xml,其中包含一个带有 Id 的 ImageView,应将其添加到 public.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/menu_item_selector" >
<ImageView
android:id="@+id/index_row_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="@dimen/padding_small" />
<ImageView
android:id="@+id/index_row_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/index_row_search" />
<TextView
android:id="@+id/index_row_caption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/index_row_icon"
android:layout_toRightOf="@id/index_row_search"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceLarge" />
这是 public.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<public type="string" name="no_internet" id="0xAA0a0001" />
<public type="id" name="index_row_search" id="0xAA0b0015" />
</resources>
eclipse 在我添加了“index_row_search” id 的行上显示错误并说:
error: Public symbol id/index_row_search declared here is not defined!
但正如您在上面的布局文件中看到的,我有一个具有该 ID 的 ImageView。很奇怪上面一行的字符串id定义没有错误!
那么,我应该如何在 public.xml 中定义 View 的 Id 呢?
【问题讨论】:
-
“我需要为视图设置始终不变的 ID。我的意思是在不同的应用程序构建之间保持不变的 ID”——为什么?
-
同意 CommonsWare 的意见。另外,您是否清理/重建了您的项目?
-
@CommonsWare 因为我希望以后能够操作一些布局,而不需要重新编译,而且你知道,通过 aapt 重建资源可能会改变一些 id。
-
@CommonsWare 认为这是一个设计师喜欢稍微改变一些布局而不需要重新编译和拥有源代码的项目。所以我应该为他提供一个 classes.dex 文件,该文件使用始终不变的 id,在重建时不会改变。
-
@Eric 我的问题与这个问题有关:stackoverflow.com/questions/9348614/…
标签: android android-layout view android-xml