2016-09-28 4 views
0

問題があります。 (私は3を持っている私は、ボタンなどでクラスDrawingViewを作成し、活動に私はビュー、ボタンをアクティビティに追加する方法

public class PaintActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     DrawingView view=new DrawingView(this); 
     setContentView(view); 
     addContentView(view.btnEraseAll, view.params); 
    } 

、ここでそれを呼び出す私の構成は

class DrawingView extends View { 

private Paint brush = new Paint(); 
private Path path = new Path(); 
public Button btnEraseAll, btnAccept, btnBack; 
public ViewGroup.LayoutParams params; 

public DrawingView(Context context) { 

    super(context); 
    brush.setAntiAlias(true); 
    brush.setColor(Color.BLACK); 
    brush.setStyle(Paint.Style.STROKE); 
    brush.setStrokeJoin(Paint.Join.ROUND); 
    brush.setStrokeWidth(22f); 
    btnEraseAll = new Button(context); 
    btnEraseAll.setText("Clear"); 
    btnAccept = new Button(context); 
    btnAccept.setText("Accept"); 
    btnBack = new Button(context); 
    btnBack.setText("Back"); 
    params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT); 
    btnEraseAll.setLayoutParams(params); 
    btnAccept.setLayoutParams(params); 
    btnBack.setLayoutParams(params); 
    btnEraseAll.setOnClickListener(new View.OnClickListener() { 

     @Override 

     public void onClick(View view) { 
      path.reset(); 
      postInvalidate(); 
     } 

    }); 

} 

@Override 

protected void onDraw(Canvas canvas) { 

    canvas.drawPath(path, brush); 
} 

public boolean onTouchEvent(MotionEvent event) { 
    float pointX = event.getX(); 
    float pointY = event.getY(); 
    switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      path.moveTo(pointX, pointY); 
      return true; 
     case MotionEvent.ACTION_MOVE: 
      path.lineTo(pointX, pointY); 
      break; 
     default: 
      return false; 
    } 
    postInvalidate(); 

    return false; 

} 

}

ですが、私はより多くのボタンを追加する方法)を活動メインに?ここ iはボタン

addContentView(view.btnEraseAll, view.params); 

追加しかし、私は、このボタンを追加したい、あまりにも

btnAccept, btnBack; 

は誰も私を助けることができますか? 3つのボタンを追加したいと思います。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/monitor" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/MyCustomActionBar"> 
     <activity 
      android:name=".MainActivity" 
      android:screenOrientation="portrait"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".WiFiServiceDiscoveryActivity" /> 
     <activity android:name=".PaintActivity" /> 
    </application> 

</manifest> 

答えて

1

私は、あなたのコードのいくつかをリファクタリングして、それを容易にするようにしました。 ボタンをプログラムで作成するのではなく、.xmlで抽出した方がずっと簡単です。 DrawingViewを別のクラスにも抽出しました。私もそれをテストしたので、確かに動作します。

public class PaintActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_paint); 
     final DrawingView drawingView = (DrawingView) findViewById(R.id.drawing_canvas); 
     View clearBtn = findViewById(R.id.btn_clear); 
     clearBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       drawingView.reset(); 
      } 
     }); 
    } 
} 

class DrawingView extends View { 

    private Paint brush = new Paint(); 
    private Path path = new Path(); 

    public DrawingView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    private void init() { 
     brush.setAntiAlias(true); 
     brush.setColor(Color.BLACK); 
     brush.setStyle(Paint.Style.STROKE); 
     brush.setStrokeJoin(Paint.Join.ROUND); 
     brush.setStrokeWidth(22f); 
    } 

    public void reset() { 
     path.reset(); 
     postInvalidate(); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     canvas.drawPath(path, brush); 
    } 

    public boolean onTouchEvent(MotionEvent event) { 
     float pointX = event.getX(); 
     float pointY = event.getY(); 
     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       path.moveTo(pointX, pointY); 
       return true; 
      case MotionEvent.ACTION_MOVE: 
       path.lineTo(pointX, pointY); 
       break; 
      default: 
       return false; 
     } 
     postInvalidate(); 
     return false; 
    } 
} 

activity_paint.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"> 

    <com.example.myapplication.DrawingView 
     android:id="@+id/drawing_canvas" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <Button 
     android:id="@+id/btn_clear" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:text="Clear"/> 

    <Button 
     android:id="@+id/btn_accept" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:text="Accept"/> 

    <Button 
     android:id="@+id/btn_back" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="Back"/> 

</RelativeLayout> 

ids.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <item name="btn_clear" type="id"/> 
    <item name="btn_accept" type="id"/> 
    <item name="btn_back" type="id"/> 
</resources> 

ます。また、ここからプロジェクト全体をダウンロードすることができます。http://expirebox.com/download/130bc35765d0d4704c0e105627077281.html

+0

働いていません。私はViewGroupはatackを見なければならないと思う? – Rodriquez

+0

何か問題がありますか?あなたは何を達成するのが良いか説明できますか? – vovahost

+0

DrawingViewのコード全体を投稿してください。 – vovahost

関連する問題