android - 如何将 id 添加到 public.xml?

标签 android android-layout view android-xml

在我的项目中,我需要为 View 设置始终不变的 ID。我的意思是在不同的应用程序构建之间保持不变的 ID。经过一番调查后,我发现它可以通过使用 values/public.xml 来实现,并且该文件中声明的任何 id 在未来的构建中都不会改变。现在的问题是我无法在某些布局文件中定义 View 的 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 的行上显示错误并说:

错误:未定义此处声明的公共(public)符号 id/index_row_search!

但是正如您在上面的布局文件中看到的,我有一个具有该 ID 的 ImageView。奇怪上面一行的字符串id定义没有错误!

那么,在public.xml中应该如何定义一个View的Id呢?

最佳答案

在 values 中创建一个新的 xml ids.xml,所有 id 值都是常量。

<resources>
  <item type="id" name="index_row_search" />
</resources>

我认为最好从 R.java 将资源 ID 复制到 public.xml,这是第一次避免像entry index is large than available symbols这样的错误,同时保留 type of resource保持一致。

关于android - 如何将 id 添加到 public.xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12334857/

相关文章:

java - Eclipse 无法识别 Android 设备

java - 如何遍历 R.java 类的 id 属性?

java - 如何在我的自定义 osmdroid map 上设置拖动开始和结束监听器?

android - 在 Android 中以编程方式添加 TableRow

ruby-on-rails - Rails 3在另一个 Controller 中呈现部分形式

android - Android View 的 ID 可以在多个 Activity 之间安全共享吗?

c# - 资源.Designer.cs : "Resource ... does not contain a definition for ..."

android - 如何在android中修改来电和去电屏幕 View

java - 如何在 WebView 上设置渐变背景

java - 线程完成时更改布局