Android main.xml隐藏视图帮助
我无法同时显示setContentView(R.layout.main)和视图。我认为我没有正确理解这个概念。谁能告诉我哪里出了问题。多谢各位//请阅读代码中的注释 我正在尝试使用BitmapFactory在main.xml上显示图像Android main.xml隐藏视图帮助,android,android-emulator,android-ndk,android-manifest,Android,Android Emulator,Android Ndk,Android Manifest,我无法同时显示setContentView(R.layout.main)和视图。我认为我没有正确理解这个概念。谁能告诉我哪里出了问题。多谢各位//请阅读代码中的注释 我正在尝试使用BitmapFactory在main.xml上显示图像 public class TryGraph extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bund
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文件是
当您调用
setContentView
时,您正在告诉设备加载应显示给用户的整个布局。在大多数情况下,此视图将占据整个屏幕。现在这个布局文件被认为是根文件,可能包含子视图,其中一个应该是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>