xml中的ImageButton语法

时间:2013-09-11 12:32:41

标签: android xml

我正在为我的应用添加一个图像按钮 我试图使用这两种方式,但两种方式都不起作用:

<ImageButton  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" />

和第二种方式:

<ImageButton  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
</ImageButton>

它给了我这个错误“元素类型imagebutton必须后跟属性规范&gt;或/&gt;

这是我的xml文件:

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
        android:background="#FFCCCCCC"
        android:orientation="vertical"
        tools:context=".MainActivity" >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:orientation="horizontal" >
            <ImageButton
                android:id="@+id/new_btn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:contentDescription="@string/start_new"
                android:src="@drawable/new_pic"/>
        </LinearLayout>  
    </LinearLayout>

2 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

<ImageButton
    android:contentDescription="@string/test_str"
    android:id="@+id/listener"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:drawable/ic_dialog_info"
/>

答案 1 :(得分:0)

而不是通过手动编码定义组件。尝试通过GUI使用它。它会自动为任何组件添加必需的属性,并在布局中显示错误。希望你明白我的意思。
如下所示: 如果您通过GUI

创建,这是一个默认按钮
 <Button
        android:id="@+id/button1" // mandatory you just miss it
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/helloworld"
        android:layout_below="@+id/helloworld"
        android:layout_marginLeft="46dp"
        android:layout_marginTop="100dp"
        android:text="Button" />