如何正确扩展的ImageButton? [英] How to extend ImageButton correctly?

查看:168
本文介绍了如何正确扩展的ImageButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望延长的ImageButton类的功能,在一个新的类,我选择称之为DialButton。要开始了,我的SimPy延伸的ImageButton,并添加什么新东西。在我看来,这应该是等同于正常的ImageButton类。

I want to extend the functionality of the ImageButton class, in a new class I choose to call "DialButton". To start off, I simpy extend ImageButton, and added nothing new. In my mind, this should be identical to the normal ImageButton class.

package com.com.com;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageButton;

public class DialButton extends ImageButton{

    public DialButton(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public DialButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public DialButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

}

我在XML(不要担心有关文件的剩余部分,​​它无关)插入DialButton

I insert a DialButton in XML (dont worry about the rest of the file, its irrelevant)

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">
    <TableRow
    android:layout_weight="1">
        <DialButton
            android:id="@+id/button1"
            android:background="@drawable/dialpad"
            android:layout_weight="1"/>

和使用XML在我的活动:

And use the XML in my activity:

package com.com.com;

import android.app.Activity;
import android.os.Bundle;

public class Android3 extends Activity {

    DialButton button1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.maintable);   
    }
}

一切编译并安装在模拟器,但是当应用程序启动时,它给我的力量关闭。如果我的XML组件改变从DialButton到的ImageButton,一切工作正常。为什么是这样?导致该DialButton组件崩溃的应用程序中的ImageButton类和我的DialButton车厢之间有什么区别?

Everything compiles and installs in the emulator but when the app starts it force closes on me. If I change the XML component from a DialButton to an ImageButton, everything works fine. Why is this? What is the difference between the ImageButton class and my DialButton class that causes the DialButton component to crash the app?

推荐答案

在你的布局文件,你必须为你定制的小部件提供了一个完全合格的名称如下...

In your layout file, you have to provide a 'fully-qualified' name for your custom widgets as follows...

<com.mycompany.myapp.DialButton
    android:id="@+id/button1"
    android:background="@drawable/dialpad"
    android:layout_weight="1"/>

这篇关于如何正确扩展的ImageButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆