简体   繁体   中英

Android relative layout issue

hello there today I tried to make a relative layout for android but I m getting errors in R.java: "Unexpected end of declaration" 8 times I passed a hour on it and I still didnt find anything

here is the code (I m sure its about xml not java code)

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:id="@+id/playScrollView1">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:id="@+id/playRelativeLayout1">

        <TextView
            android:layout_height="wrap_content"
            android:text="Convert to binary"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="fill_parent"
            android:id="@+id/TextView1"
            android:layout_alignParentTop="true"/>

        <Button
            android:layout_height="wrap_content"
            android:text="0"
            android:layout_width="wrap_content"
            android:layout_below="@id/TextView1"
            android:id="@+id/1"/>

        <Button
            android:layout_height="wrap_content"
            android:text="0"
            android:layout_width="wrap_content"
            android:id="@+id/2"
            android:layout_below="@id/1"/>

    </RelativeLayout>
</ScrollView>

thanks in advance!

You need to use

  <Button
        android:layout_height="wrap_content"
        android:text="0"
        android:layout_width="wrap_content"
        android:layout_below="@+id/TextView1" // missing+
        android:id="@+id/button1"/> // change id to button1 not 1

Same for other button

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

Any View object may have an integer ID associated with it, to uniquely identify the View within the tree. When the application is compiled, this ID is referenced as an integer, but the ID is typically assigned in the layout XML file as a string, in the id attribute.

Example :

android:id="@+id/my_button"

The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file)

For relative layout

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

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/playScrollView1">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:id="@+id/playRelativeLayout1">

        <TextView
            android:layout_height="wrap_content"
            android:text="Convert to binary"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="fill_parent"
            android:id="@+id/TextView1"
            android:layout_alignParentTop="true"/>

        <Button
            android:layout_height="wrap_content"
            android:text="0"
            android:layout_width="wrap_content"
            android:layout_below="@+id/TextView1"
            android:id="@+id/button1"/>

        <Button
            android:layout_height="wrap_content"
            android:text="0"
            android:layout_width="wrap_content"
            android:id="@+id/2"
            android:layout_below="@+id/button1"/>

    </RelativeLayout>
</ScrollView>

Its just naming convention

Just change your id name of Buttons @+id/1 to @+id/One

it shall not be Numeric only it shall not start with Numeric coz it will generate Field in R.java

like the following

    public static final int 1=0x7f0a0032;
    public static final int 2=0x7f0a0033;

As per convention it is WRONG a big WRONG

So Your code shall go like this

        <Button
            android:id="@+id/One"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/TextView1"
            android:text="0" />

        <Button
            android:id="@+id/Two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/One"
            android:text="0" />

OR completely

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/playScrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:id="@+id/playRelativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="20dp"
        android:paddingRight="20dp" >

        <TextView
            android:id="@+id/TextView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="Convert to binary"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Button
            android:id="@+id/One"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/TextView1"
            android:text="0" />

        <Button
            android:id="@+id/Two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/One"
            android:text="0" />
    </RelativeLayout>

</ScrollView>

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