既然是继承View的 可以在XML布局里面直接设置背景。 <com.android.widget.MyView android:id="@+id/myview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/background" //设置背景 android:layout_marginTop="11px" android:layout_marginLeft="506px"/> 如果想在代码里面设置背景,结合上面的XML文件: private MyView mView; mView = (MyView) findViewById(R.id.myview); mView.setBackgroundResource(R.drawable.background); 如果想在自定义控件的代码中直接设置背景,需要先在构造函数中获取背景图片,然后在 onDraw中画背景图片。 mBackGround = ((BitmapDrawable) this.getResources().getDrawable(R.drawable.background)).getBitmap(); //获取背景图片 Paint mPaint = new Paint(); canvas.drawBitmap(mBackGround, 0, 0, mPaint); //画背景图片
android中扩展View类的背景设置
最新推荐文章于 2024-06-05 01:05:49 发布