Android 如何在活动中实现视图?
我有Feldlayout类,Feldlayout.xml是Feldlayout类的布局,还有一个View类,可以用来在屏幕上画线。现在我想在feldlayout.xml布局上创建这些行,但我不知道如何连接我的类。那我该怎么做呢 主类 Feldlayout.xml主布局Android 如何在活动中实现视图?,android,xml,android-activity,view,Android,Xml,Android Activity,View,我有Feldlayout类,Feldlayout.xml是Feldlayout类的布局,还有一个View类,可以用来在屏幕上画线。现在我想在feldlayout.xml布局上创建这些行,但我不知道如何连接我的类。那我该怎么做呢 主类 Feldlayout.xml主布局 您可以在布局xml中使用自定义视图: <com.example.volleyballapp.ViewFeld android:layout_width="....." android:layout_heigh
您可以在布局xml中使用自定义视图:
<com.example.volleyballapp.ViewFeld
android:layout_width="....."
android:layout_height="....."
etc..... />
我做到了,但我不能划清界限,我怎么能在其他人面前得到这个呢?
<?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>
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;
}
}
<com.example.volleyballapp.ViewFeld
android:layout_width="....."
android:layout_height="....."
etc..... />