2012-07-05 59 views
0

我在使用画布绘制我的数学公式和文本以及在屏幕底部显示输入数据以获得结果的文本框时遇到问题。在我目前的设置中,我可以有按钮和文本框,或方程式和变量名称。下面是从Java类在Android上使用多个内容视图(XML和画布)

package com.soraingraven.suprRef; 

import java.io.IOException; 
import java.io.InputStream; 

import android.app.Activity; 
import android.content.Context; 
import android.content.res.AssetManager; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Typeface; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 

public class PerfectGasLaw extends Activity { 
    class RenderView extends View { 
     //For the Text 
     Paint paint; 
     Typeface font; 

     //For the pics 
     Bitmap PGL; 

     public RenderView(Context context) { 
      super(context); 
      paint = new Paint(); 

      //Attempt to load the bitmaps 
      try {    
       AssetManager assetManager = context.getAssets(); 
       InputStream inputStream = assetManager.open("PerfGasLaw.png"); 
       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inPreferredConfig = Bitmap.Config.ARGB_4444; 
       PGL = BitmapFactory.decodeStream(inputStream, null, options); 
       inputStream.close(); 
       Log.d("BitmapText", "bobargb8888.png format: " + PGL.getConfig()); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
       //we should really close our input streams here. 
      } 
     } 

     //Drawing text and pics 
     protected void onDraw(Canvas canvas) { 
      paint.setColor(Color.BLUE); 
      paint.setTypeface(font); 
      paint.setTextSize(32); 
      paint.setTextAlign(Paint.Align.LEFT); 
      canvas.drawText("P = Pressure in Atmospheres", 25, 350, paint); 
      canvas.drawText("V = Volume in Liters", 25, 400, paint); 
      canvas.drawText("n = Number of moles", 25, 450, paint); 
      canvas.drawText("R = Gas Constant", 25, 500, paint); 
      canvas.drawText(" - (0.0821 Liter-Atmospheres/K/mole)", 50, 550, paint); 
      canvas.drawText("T = Temperature in K", 25, 600, paint); 
      canvas.drawText("If Constant Pressure - V1/V2 = T1/T2", 25, 650, paint); 
      canvas.drawText("If Constant Temperature - P1/P2 = V2/V1", 25, 700, paint); 
      canvas.drawText("If Constant Volume - P1/P2 = T1/T2", 25, 750, paint); 

      canvas.drawBitmap(PGL, 25, 25, null); 
      invalidate(); 
     } 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     setContentView(new RenderView(this)); 
    } 
} 

和XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" > 


    <EditText 
     android:id="@+id/edit_message" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:layout_weight="1" 
     android:ems="10" 
     android:hint="@string/edit_message" > 

     <requestFocus /> 
    </EditText> 


    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:text="@string/button_send" /> 

</LinearLayout> 

请帮助我这样做我的代码。此外,如果任何人都可以告诉我如何从这样的文本字段获取输入信息,这将会有所帮助。此外,我正在尝试尽可能使用最少量的XML。

回答

1

您可以从View继承自己的子类,例如EquationView,以便该视图具有您的onDraw方法()。

然后,在你把你的视图中的XML,像

<mypackage.equation.EquationView ... 
    android:layout_width=... 
    android:layout_height=... 
/> 

EditText沿和Button

+0

将EquationView在我的代码是从不同的RenderView?如果这有所作为,这是从列表中选择的单独活动。 – user1183668 2012-07-05 17:44:38

+0

这将是'RenderView extends View',它将位于Activity的xml文件中。在Activity中,您可以通过findViewById找到渲染视图,以便您可以在需要时访问视图。 – 2012-07-05 19:33:12

+0

将包放入我的xml文件时,出现此错误。 说明\t \t资源路径\t \t位置类型 属性名称“com.soraingraven.suprRef.RenderView”与元素类型“包”相关联必须跟在“=”字符。 \t \t main.xml中/超级参考/ RES /布局\t线109 \t的Android XML格式问题 我已经把像这样 <包com.soraingraven.suprRef.RenderView 机器人:ID =“@ + ID /可拉伸” \t \t \t机器人:layout_width = “WRAP_CONTENT” \t \t \t机器人:layout_height = “WRAP_CONTENT”/> 它是一个相对布局内如果有差别 – user1183668 2012-07-06 02:56:32

相关问题