1

基本上,我正在实现一个系统,其中视图由按钮和表面视图组成。这个表面视图是在主要活动的类中以编程方式定义的。现在我需要将其添加为 xml 文件的一部分,因为它是自己的标签。

当前的xml代码是:

<RelativeLayout 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="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

   <Button
        android:id="@+id/downbutton"
        android:layout_width="100dip"
        android:layout_height="75dip"
        android:layout_alignLeft="@+id/upbutton"
        android:layout_centerVertical="true"
        android:text="Down" />

    <Button
        android:id="@+id/rightbutton"
        android:layout_width="100dip"
        android:layout_height="75dip"
        android:layout_alignBottom="@+id/leftbutton"
        android:layout_marginLeft="80dp"
        android:layout_toRightOf="@+id/upbutton"
        android:text="Right" />

    <Button
        android:id="@+id/leftbutton"
        android:layout_width="100dip"
        android:layout_height="75dip"
        android:layout_above="@+id/downbutton"
        android:layout_marginRight="80dp"
        android:layout_toLeftOf="@+id/upbutton"
        android:text="Left" />

    <Button
        android:id="@+id/upbutton"
        android:layout_width="100dip"
        android:layout_height="75dip"
        android:layout_above="@+id/rightbutton"
        android:layout_centerHorizontal="true"
        android:text="Up" />

    <com.example.gui.GridSurfaceView
        android:id="@+id/gridsurfaceview"
        android:layout_width="fill_parent"
        android:layout_height="300dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="#cbfff4" />

</RelativeLayout>

这里,“com.example.gui.GridSurfaceView”是指表面视图类。但是,我的表面视图类现在已放置在主活动的类中并且无法删除(因为表面视图类需要访问该活动的一些变量)。当表面视图有它自己的类时,视图看起来很好,但现在,这个 xml 引起了一些问题。我将如何更改 xml 以再次包含表面视图?

编辑 - 好吧,既然问了,这里也是活动代码。由于代码太大,我已经包含了包含表面视图的活动部分。注意,主要活动的名称是“Robotremote”,表面视图是“GridSurfaceView”

public class GridSurfaceView extends SurfaceView implements Runnable {

SurfaceHolder GridSurfaceHolder;
Thread gridThread = null;
boolean isRunning = false;
Paint paint;
Typeface mFont = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
Paint red;

public GridSurfaceView(Context context) {
    // TODO Auto-generated constructor stub
    super(context);
    paint = new Paint();
    paint.setTextSize(14);
    paint.setTypeface(mFont);
    paint.setAntiAlias(true);
    red = new Paint();
    red.setColor(getResources().getColor(R.color.position));
    GridSurfaceHolder = getHolder();
    gridThread = new Thread(this); //allows for the usage of the run method of     this class. 
    gridThread.start();
}

public void pause(){
    isRunning = false;
    while(true){
        try {
            gridThread.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        break;
        }
        gridThread = null; 
}

public void resume(){
    isRunning = true;
    gridThread = new Thread(this);
    gridThread.start();
}

@Override
public void run() {
    // TODO Auto-generated method stub
    while(isRunning){
        if(!GridSurfaceHolder.getSurface().isValid())
            continue;

        Canvas gridcanvas = GridSurfaceHolder.lockCanvas();
        gridtobedrawn = com.example.gui.Robotremote.getGrid();
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(3);
        gridcanvas.drawRect(130, 130, 180, 180, paint);
        gridtobedrawn.draw(gridcanvas, paint);
        gridcanvas.drawCircle(xCoord, yCoord, (30*0.45f), red);

    }

}

}
4

2 回答 2

1

以下是您应该如何克服这个问题:

在您的自定义视图中定义一个监听器接口,并定义一个监听器设置器。并在您活动的 onCreate 方法中拉出视图,将其转换为 GridSurfaceView 并向其添加一个实现您的接口侦听器的匿名类(例如按钮的 setOnCLickListener)

并且在接口的实现中,您可以使用活动的任何字段

--------------------------------------------------------------------

编辑

使用侦听器将 GridSurfaceView 解耦以将 xCoord 和 yCoord 注入到您的视图中,从而使您的 GridSurfaceView 与您的 Activity 分开:

  1. 在 GridSurfaceView 中定义一个接口来处理 xCoord 、 yCoord 注入:

    public interface CoordsProvider {
        public double getXCoord();
        public double getYCoord(); // am assuming your xCoord and yCoord are doubles
    }
    
  2. 在 GridSurfaceView 中定义一个字段提供者

    CoordsProvider provider;
    
  3. 在 GridSurfaceView 中为您的提供者定义一个设置器

     public void setCoordsProvider(CoordsProvider p){
          provider = p;
     }
    
  4. 在您的运行方法中,而不是直接使用 xCoords 和 yCoords,而是像这样调用您的 coords 提供程序:

    gridcanvas.drawCircle(provider.getXCoord(), provider.getYCoord(), (30*0.45f), red);
    
  5. 修改您的 GridSurfaceView 构造函数,以便它可以从 xml 膨胀。您所要做的就是向构造函数添加一个参数:

     public GridSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
    ...//same code
    
  6. 将您的 GridSurfaceView 放在您的 xml 布局文件中,就像您在帖子中所做的那样

  7. 在 setContentView 之后使用 findViewById 将其拉入 Activity 的 onCreate 方法中:

     @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout_id);
        gridSurfaceView = (GridSurfaceView) findViewById(R.id.gridsurfaceview);
    
        gridSurfaceView.setCoordsProvider(new CoordsProvider(){
            @Override
            public double getXCoord(){return xCoord;}
    
            @Override
            public double getYCoord(){return yCoord;}
        });
    
于 2013-09-26T17:46:01.293 回答
0

当它被放置为内部类时,您无法定义自定义视图。想想它必须是独立的,并且要实例化自定义视图,它必须实例化整个 Activity。

我建议在 XML 中使用 FrameLayout 或 ViewStub 占位符,并以编程方式扩展自定义视图。

于 2013-09-26T17:33:23.783 回答