简体   繁体   中英

Layout XML View IDs and R problems

I am creating a layout in XML, and I have noticed this very strange occurrence. It has never happened before, but lately it's been getting on my nerves. I kind of want to abandon Eclipse and find a better IDE because of it.

Here is my layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/action_search_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="kkm" >
    </EditText>

    <ListView
        android:id="@+id/actions_list"
        android:layout_width="90dp"
        android:layout_height="match_parent"
        android:layout_above="@id/cancel_action_selection"
        android:layout_below="@id/action_search_bar" >
    </ListView>

    <TextView
        android:id="@+id/action_preview_pane"
        android:layout_width="230dp"
        android:layout_height="match_parent"
        android:layout_alignBottom="@id/actions_list"
        android:layout_alignTop="@id/actions_list"
        android:layout_toRightOf="@id/actions_list"
        android:gravity="center_vertical|center_horizontal"
        android:text="kk" />

    <Button
        android:id="@+id/cancel_action_selection"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_alignParentBottom="true"
        android:text="Cancel" />

    <Button
        android:id="@+id/define_new_action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@id/cancel_action_selection"
        android:text="NEW" />

    <Button
        android:id="@+id/select_action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@id/define_new_action"
        android:text="OK" />

</RelativeLayout>

Here is what I'm trying to achieve: 我正在努力实现的目标

The problem is, if I try to set the ListView to be above the cancel button (via android:layout_above), it will say that it can't find the id for the cancel button.

Now, what I have noticed is that if I try and specify any ids for layouts BEFORE they are created with @+id, then I will get that strange error. What I mean by this is the following:

In the XML, apparently IDs have to be declared in order. Strange, no? I would have to declare the cancel button's id first with @+id way before I define the ListView and set its layout_above attribute.

If I try to do what I have done here, it will say that it cannot find the id for the cancel button.

I have tried cleaning the project, ending adb.exe, refreshing, etc. and all it does is corrupt my project even more. R is not even generated afterwards. I don't want to have to go in a loop problem after problem because Eclipse can't handle things like this properly.

Does anyone have a solution for this? Or a better IDE?

http://developer.android.com/guide/topics/ui/layout/relative.html

RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID) . So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on.

So you are placing listview relative to the button cancel. So you need to define button with a id. Position the listview relative to the button.

http://developer.android.com/guide/topics/ui/layout/linear.html

There is an example in the links posted both for linearlayout and relative layout have a look at it.

Edit:

If you have errors in your resource files R.java will not be generated. So make sure you do not have errors in your resource files and then clean and build the project.

To clarify the confusion. Note android:layout_above="@+id/button1"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".OtherScreenActivity1" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="24dp"
            android:text="Button" />

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/button1"
             >
        </ListView>

</RelativeLayout>

Same as above

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".OtherScreenActivity1" >

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/button1"
            android:layout_alignParentTop="true" >

        </ListView>
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="24dp"
            android:text="Button" />

</RelativeLayout>

You have this

  android:layout_above="@id/cancel_action_selection"

Use

  android:layout_above="@+id/cancel_action_selection"

http://developer.android.com/guide/topics/ui/declaring-layout.html .

Look at the attribute id in the above link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM