简体   繁体   English

Android相对布局问题

[英]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 你好,今天我尝试为android进行相对布局,但我在R.java中遇到错误:“我的声明结束意外” 8次,我花了一个小时,仍然找不到任何内容

here is the code (I m sure its about xml not java code) 这是代码(我确定它是关于xml而不是Java代码)

<?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 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. 任何View对象都可以具有与其关联的整数ID,以唯一地标识树中的View。 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. 编译应用程序时,此ID被引用为整数,但是ID通常在布局XML文件中的id属性中作为字符串分配。

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. 字符串开头的符号(@)表示XML解析器应解析并扩展ID字符串的其余部分,并将其标识为ID资源。 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) 加号(+)表示这是一个新的资源名称,必须创建并添加到我们的资源中(在R.java文件中)

For relative layout 对于相对布局

http://developer.android.com/guide/topics/ui/layout/relative.html 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 只需将Buttons @+id/1 ID名称更改为@+id/One

it shall not be Numeric only it shall not start with Numeric coz it will generate Field in R.java 它不能是数字 ,而不能数字 coz 开头 ,它将在R.java中生成Field

like the following 像下面

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

As per convention it is WRONG a big WRONG 按照惯例,这是错误的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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