0

setContentView(R.layout.main) と View の両方を一緒に表示できません。私はコンセプトを正しく理解していないと思います。誰かが私がどこで間違っているのか説明してもらえますか? ありがとうございました。//コード内のコメントを読んでください

BitmapFactory を使用して main.xml に画像を表示しようとしています。

   public class TryGraph extends Activity 
 {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);//I think this is where I need your help
    setContentView(new myView(this));//I want this to be displayed in main.xml
}

private class myView extends View{

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

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sinewave);
        Bitmap resizedBitmap = Bitmap.createBitmap(myBitmap, 0, 0,
                300, 143);
        canvas.drawBitmap(resizedBitmap, 60, 50, null);
        Paint myPaint = new Paint();
        myPaint.setColor(Color.RED);
        myPaint.setStyle(Paint.Style.STROKE);
        myPaint.setStrokeWidth(5);
        canvas.drawRect(250,255,260,250, myPaint);

    }
}

}

XMLファイルは

4

1 に答える 1

1

呼び出すsetContentViewと、ユーザーに表示する必要があるレイアウト全体をデバイスにロードするように指示されます。ほとんどの場合、このビューは画面全体を占めます。現在、このレイアウト ファイルはルートと見なされ、子ビューが含まれる場合があり、そのうちの 1 つが ImageView である必要があります。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView   
    android:id="@+id/banner"
    android:text="hello world"
    >  
<ImageView
    android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/sampleimage"
    />
<?LinearLayout>

この ImageView は、 を使用してコードからアクセスできるようになり、findViewById(R.id.myImageView)を使用しBitmapFactoryてイメージを設定できます。画像を変更しない場合は、レイアウトファイルに設定するだけですandroid:src="@drawable/sampleimage"

于 2010-08-07T03:38:57.570 に答える