您的位置:首页 > 移动开发 > Android开发

android中扩展View类的背景设置

2015-09-23 17:22 399 查看
既然是继承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); //画背景图片
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: