我使用3行创建一个三角形,我使用的是drawView。它出来很好。但重点是我正在将setContentView作为setContentView(drawView)而不是setContentView(R.layout.xml)。结果我看不到xml中的文本框和按钮。我知道我必须在我的布局中创建一个DrawView实例,然后执行setContentView(R.layout.xml)。我试过这样做,但我仍然收到错误。
请帮忙!
我的xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.example.testapp.DrawView
android:id="@+id/draw_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
我的活动代码
package com.example.testapp;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.testapp.MESSAGE";
DrawView drawView;
protected void onCreate(Bundle savedInstanceState) {
System.out.println(" ONCREATE+++");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//drawView = (DrawView)findViewById(R.id.draw_view);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
class DrawView extends View {
Paint paint1 = new Paint();
Paint paint2 = new Paint();
Paint paint3 = new Paint();
public DrawView(Context context) {
super(context);
System.out.println(" DrawView1");
paint1.setColor(Color.RED);
paint2.setColor(Color.BLUE);
paint3.setColor(Color.GREEN);
System.out.println(" DrawView2");
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
System.out.println(" onDraw");
canvas.drawLine(150, 640, 550, 300, paint1); // Slant Line
canvas.drawLine(150, 640, 550, 640, paint2); // Horizontal Line
canvas.drawLine(550, 300, 550, 640, paint3); // Vertical Line
canvas.drawCircle(150, 655, 15, paint2);// Circle for roller support
canvas.drawLine(550, 640, 565, 655, paint2); // Line1 for hinge support
canvas.drawLine(550, 640, 535, 655, paint3); // Line 2 for hinge support
// canvas.drawRect(535, 625, 555, 665, paint2); //Rectangle depicting Hinge Support
canvas.drawLine(660, 300, 550, 300, paint2);// Load Length
canvas.drawLine(550/*Start*/, 300, 565, 280, paint2); // Load Arm1
canvas.drawLine(550, 300, 565, 320, paint2); // Load Arm2
}
}
}
谢谢!