小弟初学android,今天看到一个样例public class shiyan extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new myview(this)); }public class myview extends View {
Bitmap bmp;
Bitmap bmp2;
public myview(Context context) {
super(context);
// TODO Auto-generated constructor stub
Resources res=context.getResources();
bmp=BitmapFactory.decodeResource(res,R.drawable.blackzhu);
bmp2=BitmapFactory.decodeResource(res,R.drawable.bitmap1);
//Resources resources = getContext().getResources();
//Drawable btnDrawable = resources.getDrawable(R.drawable.bitmap1);
//Layout.setBackgroundDrawable(btnDrawable);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bmp2, 0, 0,new Paint());
canvas.drawBitmap(bmp, 40, 130,new Paint());
}}程序中Acivity的setContentView](new myview(this),调用了myview,myview为一个新建的类在这个程序中myview与Acivity是什么关系,在myview中能不能定义控件,比如TextView,怎样定义
请哪位前辈指教一下,不胜感激!
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new myview(this)); }public class myview extends View {
Bitmap bmp;
Bitmap bmp2;
public myview(Context context) {
super(context);
// TODO Auto-generated constructor stub
Resources res=context.getResources();
bmp=BitmapFactory.decodeResource(res,R.drawable.blackzhu);
bmp2=BitmapFactory.decodeResource(res,R.drawable.bitmap1);
//Resources resources = getContext().getResources();
//Drawable btnDrawable = resources.getDrawable(R.drawable.bitmap1);
//Layout.setBackgroundDrawable(btnDrawable);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bmp2, 0, 0,new Paint());
canvas.drawBitmap(bmp, 40, 130,new Paint());
}}程序中Acivity的setContentView](new myview(this),调用了myview,myview为一个新建的类在这个程序中myview与Acivity是什么关系,在myview中能不能定义控件,比如TextView,怎样定义
请哪位前辈指教一下,不胜感激!
解决方案 »
- Android sax解析出错(求助)
- 求助:一个android程序中有一些初始配置要保存下来,在程序里改这些配置,现在不清这些配置怎么保存,resource raw assets 好象都是只读的
- 求几个国外的android开源网站????????
- Android源代码所在的目录在什么地方
- 『求助』可不可麻烦哪位大侠给介绍下Handler、Message、handleMessage、handleThread的关系区别和用法。。。
- java的线程里有没有类似vc的CriticalSection
- 装Android的机型有哪些?哪里有比较全面的数据?
- 分享一个原创Android 单机版斗地主源码
- android studio打开源码出现android.annotation里面的类飘红
- 求交叉编译指导
- android webservice的问题?
- android
setContentView就是创建activity的时候要设置显示的界面布局,这里设为一个view,你也可以自己去制作layout,然后setContentView进去就是你要的布局了。如果你要textview,那可以在main.xml(这个是创建空工程自带的,你也可以自己定义)中,添加<TextView android:id="@+id:text">
</TextView>这样在setContentView时就直接填id就可以了。
setContentView(R.id.text);
可以在myview中增加TextView控件,要先设置一个layout,把textview放在layout上,并且把layout设置为shiyan的view。
public class AndTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new myview(this).layout);
}
public class myview extends View {
Bitmap bmp;
Bitmap bmp2;
TextView tv;
LinearLayout layout;
public myview(Context context) {
super(context);
// TODO Auto-generated constructor stub
Resources res=context.getResources();
bmp=BitmapFactory.decodeResource(res,R.drawable.icon);
bmp2=BitmapFactory.decodeResource(res,R.drawable.icon);
//Resources resources = getContext().getResources();
//Drawable btnDrawable = resources.getDrawable(R.drawable.bitmap1);
//Layout.setBackgroundDrawable(btnDrawable);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT); layout = new LinearLayout(AndTest.this);
layout.setLayoutParams(lp);
tv = new TextView(AndTest.this);
tv.setText("Hello world!\nYou are welcome!");
layout.addView(tv, lp1);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bmp2, 0, 0,new Paint());
canvas.drawBitmap(bmp, 40, 130,new Paint());
} }
}