如何将 id 添加到 public.xml? [英] How to add id to public.xml?

查看:32
本文介绍了如何将 id 添加到 public.xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我需要将始终不变的 ID 设置为视图.我的意思是在不同的应用程序版本之间保持不变的 ID.经过一番调查,我发现它可以通过使用 values/public.xml 来实现,并且该文件中声明的任何 id 在未来的构建中都不会改变.现在的问题是我无法在某些布局文件中定义视图的 id.这是我的 layout.xml,包含一个带有 Id 的 ImageView,应该添加到 public.xml 中:

In my project, I needed to set always-constant IDs to Views. I mean IDs that are constant between different app builds. After some investigation I found that it can be achieved by using values/public.xml and any id declared in that file would not change on future builds. Now the problem is that I can't define an id of a view in some layout file. This is my layout.xml containing an ImageView with an Id which should be added to 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 文件:

and this is public.xml file:

<?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 的行上显示了一个错误,并说:

eclipse shows an error on the line where I have added id of "index_row_search" and says:

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

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

but as you can see in above layout file, I have an ImageView with that id. It's wondering that the string id definition one line above has no error!

那么,我应该如何在 public.xml 中定义视图的 ID?

So, how should I define Id of a View in public.xml?

推荐答案

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

Create a new xml ids.xml in values with all the id values which are to be constant.

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

我认为如果您将资源 id 从 R.java 复制到 public.xml 会更好,这是第一次避免诸如条目索引大于可用符号之类的错误,并保持资源类型要保持一致.

I think its better if you copy the resource ids to public.xml from the R.java, the first time to avoid errors like entry index is larger than available symbols and also to keep the type of resource to be consistent.

这篇关于如何将 id 添加到 public.xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆