Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

android - How to add id to 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" />

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 shows an error on the line where I have added id of "index_row_search" and says:

error: Public symbol id/index_row_search declared here is not defined!

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!

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...