【问题标题】:making Custome UI (by extend View)制作自定义 UI(通过扩展视图)
【发布时间】:2013-04-10 06:55:56
【问题描述】:

我制作了 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>

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

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

【问题讨论】:

  • 会不会是构造函数?尝试不带参数...

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


【解决方案1】:

问题在这里:

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

您需要在此处提供活动类。我猜你提供的是你的 CustomView 的类

【讨论】:

  • 我将其更改为如下 但一秒钟后仍然关闭。请帮我解决dis问题。
  • 在更改了 manfist 中的活动类和 CustomUi.java 文件的构造函数后,你现在正在工作
【解决方案2】:

您需要在 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);

希望对你有帮助..

【讨论】:

    猜你喜欢
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    相关资源
    最近更新 更多