【问题标题】:How can I implement a View in my Activity?如何在我的活动中实现视图?
【发布时间】:2014-08-04 19:00:32
【问题描述】:

我有 Feldlayout 类,feldlayout.xml 是 Feldlayout 类的布局,还有一个 View 类,可以用来在屏幕上画线。我现在想在 feldlayout.xml 布局上制作这些行,但我不知道如何连接我的类。那么我该怎么做呢?

Feldlayout.class(主类)

package com.example.volleyballapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class feldlayout extends Activity{
Button b7;   

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feldlayout);
        b7 = (Button) findViewById(R.id.button7);
        b7.setOnClickListener(handler);

   }

  View.OnClickListener handler = new View.OnClickListener(){
    public void onClick(View v) {

        if(v==b7){
        Intent i = new Intent(feldlayout.this, Main_Layout.class);
        startActivity(i);
        }


    }};}

Feldlayout.xml(主布局)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/farbe"
android:orientation="vertical" >

<ImageView
  android:id="@+id/view2"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_marginBottom="70dp"
  android:layout_marginLeft="70dp"
  android:layout_marginRight="70dp"
  android:layout_marginTop="160dp"
  android:background="@drawable/whiterectangle" />

 <ImageView
     android:id="@+id/view1"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_marginBottom="74dp"
     android:layout_marginLeft="74dp"
     android:layout_marginRight="74dp"
     android:layout_marginTop="164dp"
     android:background="@drawable/volleyballfeld" />

 <ImageView
     android:id="@+id/view3meterlinie"
     android:layout_width="match_parent"
     android:layout_height="4dp"
     android:layout_alignTop="@+id/view1"
     android:layout_marginLeft="70dp"
     android:layout_marginRight="70dp"
     android:layout_marginTop="100dp"
     android:background="@drawable/dreimeterlinie" />

 <ImageView
     android:id="@+id/netz"
     android:layout_width="match_parent"
     android:layout_height="4dp"
     android:layout_alignTop="@+id/view1"
     android:layout_marginLeft="20dp"
     android:layout_marginRight="20dp"
     android:layout_marginTop="310dp"
     android:background="@drawable/netz" />

 <ImageView
     android:id="@+id/view3meterliniedef"
     android:layout_width="match_parent"
     android:layout_height="4dp"
     android:layout_alignTop="@+id/view1"
     android:layout_marginLeft="70dp"
     android:layout_marginRight="70dp"
     android:layout_marginTop="520dp"
     android:background="@drawable/dreimeterlinie" />

 <Button
     android:id="@+id/button8"
     style="@style/HomeText"
     android:layout_width="140dp"
     android:layout_height="85dp"
     android:layout_alignParentTop="true"
     android:layout_alignRight="@+id/view1"
     android:layout_marginTop="1dp"
     android:background="@drawable/bnt_black"
     android:text="Speichern" />

 <Button
     android:id="@+id/button7"
     style="@style/HomeText"
     android:layout_width="140dp"
     android:layout_height="85dp"
     android:layout_alignLeft="@+id/view2"
     android:layout_alignParentTop="true"
     android:layout_marginTop="1dp"
     android:background="@drawable/bnt_black"
     android:onClick="home"
     android:text="Zurück" />

 <Button
     android:id="@+id/button9"
     style="@style/HomeText"
     android:layout_width="140dp"
     android:layout_height="85dp"
     android:layout_alignTop="@+id/button7"
     android:layout_marginLeft="35dp"
     android:layout_toRightOf="@+id/button7"
     android:background="@drawable/bnt_black"
     android:text="Linien Anzeigen" />

 <Button
     android:id="@+id/Statistik"
     style="@style/HomeText"
     android:layout_width="140dp"
     android:layout_height="85dp"
     android:layout_alignBaseline="@+id/button8"
     android:layout_alignBottom="@+id/button8"
     android:layout_marginRight="31dp"
     android:layout_toLeftOf="@+id/button8"
     android:background="@drawable/bnt_black"
     android:text="Statistik" />

    </RelativeLayout>

ViewFeld.class(视图类,制作线条)

package com.example.volleyballapp;

 import android.content.Context;
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Paint;
 import android.graphics.Path;
 import android.util.AttributeSet;
 import android.view.MotionEvent;
 import android.view.View;

 public class ViewFeld extends View{

 private Paint paint = new Paint();
  private Path path = new Path();
  int count = 1;

public ViewFeld(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    paint.setAntiAlias(true);
    paint.setStrokeWidth(6f);           //Breite des Strichs
    paint.setColor(Color.BLACK);        //Farbe des Strichs 
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);

  }



protected void onDraw(Canvas canvas) {

    if ((count%2 != 0)){
    canvas.drawPath(path, paint);}
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:

        count++;

        if(count%2 != 1){
        path.moveTo(eventX, eventY);
      return true;
        }
        if(count%2 != 0){
        path.lineTo(eventX, eventY);
        break;
        }
    case MotionEvent.ACTION_MOVE:

      break;
    case MotionEvent.ACTION_UP:
      // nothing to do
      break;
    default:
      return false;
    }

    // Schedules a repaint.
    invalidate();
    return true;
  }




}

【问题讨论】:

    标签: android xml android-activity view


    【解决方案1】:

    您可以在布局 xml 中使用自定义视图:

    <com.example.volleyballapp.ViewFeld
        android:layout_width="....."
        android:layout_height="....."
        etc.....  />
    

    【讨论】:

    • 我做到了,但我无法画线,我怎样才能在其他人的前景中得到这个?
    猜你喜欢
    • 2016-03-11
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    相关资源
    最近更新 更多