制作自定义UI(通过扩展视图)

时间:2013-04-17 13:25:07

标签: android android-layout user-interface custom-controls custom-component

我制作了CustomUI.java文件

public class CustomUI extends View {

    Bitmap icon;
    float left=0;

    public CustomUI(Context context) {
        super(context);
        icon=BitmapFactory.decodeResource(getResources(), R.drawable.icon);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        canvas.drawBitmap(icon, left, 0, null);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
           setMeasuredDimension(250, 100);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        if(event.getAction()==event.ACTION_SCROLL){
            left=event.getX();
        }
        if(left>=200){
            //do some activity;
        }
        return true;
    }
}

我制作了布局custom_ui.xml文件

<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=".CustomUI" >

    <com.example.missedcall.CustomUI
        android:id="@+id/single_spinner"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </com.example.missedcall.CustomUI>

</RelativeLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.missedcall"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.missedcall.CustomUI"
            android:label="@string/title_activity_custom_ui" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

当我展示Graphic layout时,它很好地展示了我,但当我运行它时, 它的运行n显示空白布局并在第二个关闭应用程序后显示错误。 请帮我解决这个问题,并在android中制作自定义UI。

日志文件: http://i.stack.imgur.com/o4L6o.jpghttps://dl.dropboxusercontent.com/u/6608612/log.JPG

2 个答案:

答案 0 :(得分:0)

问题在这里:

<activity
        android:name="com.example.missedcall.CustomUI"

您需要在此处提供活动课程。我想你提供的是你的自定义视图类

答案 1 :(得分:0)

您需要在CustomUi类中创建一个具有参数AttributeSet以及上下文的构造函数。

public CustomUi(Context context, AttributeSet attrs) {
    super(context, attrs);
    icon=BitmapFactory.decodeResource(getResources(), R.drawable.icon);
}

然后在您的主类中将CustomUi声明为

CustomUi customUi = (CustomUi) findViewById(R.id.single_spinner);

希望有所帮助......