2

Eclipse を使用して Android プロジェクトを作成しました。デフォルトのシステム生成コードは次のようになります。

    package com.rmycustomclass.bengler;

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

    public class RMyCustomClassActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    }

簡単なカスタム コントロールを試してみたかったので、以下のようにコードを変更しました。Web を検索して単純なカスタム コントロールを作成すると、「ビューをサブクラス化してクラスを作成する」のように記載されています。コードを変更して以下のようにしようとしましたが、実行されておらず、常に強制終了しました。

package com.rmycustomclass.bengler;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class RMyCustomClassActivity extends View {

    private Paint myPaint;

    public RMyCustomClassActivity(Context cxt, AttributeSet attribute){

        super(cxt,attribute);
        init();

    }

    public RMyCustomClassActivity(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init();
    }



    public void init(){

        myPaint = new Paint();
        myPaint.setTextSize(12);
        myPaint.setColor(0xFF668800);
        myPaint.setStyle(Paint.Style.FILL);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawText("TEEEST", 100, 100, myPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        this.setMeasuredDimension(150,200);     
    }



}

以下は私のxmlファイルです

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        android:layout_width="fill_parent" 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_height="fill_parent" android:orientation="vertical">

        <TextView 
        android:id="@+id/textView1" 
        android:text="Enter text"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
        </TextView>

        <TableLayout 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:id="@+id/tableLayout1">
            <Button 
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Button"
                 android:layout_weight="1">
            </Button>
            <Button android:layout_width="wrap_content"  android:id="@+id/button1" android:layout_height="40dip" android:text="My button" android:layout_weight="1"></Button>
            <test.control 
              android:id="@+id/control" 
              android:layout_height="match_parent"> </test.control>
        </TableLayout>
    </LinearLayout>
4

2 に答える 2

2

Eclipse でプロジェクトを作成すると、アクティビティ (この場合は RMyCustomClassActivity) が Android マニフェストのランチャーとしてリンクされます...しかし、アクティビティを View Android ランタイムに変更したため、起動するアクティビティが見つかりません...マニフェストを参照してくださいファイルを開くと、ランチャーアクティビティとしてクラスが見つかります

于 2012-11-14T07:04:05.170 に答える
0

android:layout_width="match_parent" を追加

<test.control 
          android:id="@+id/control" 
          android:layout_height="match_parent"
          android:layout_width="match_parent"
> 
于 2012-11-14T07:02:01.820 に答える