2016-05-23 3 views
0

私はそれに画像を置くためにCanvasViewを使用します。 CanvasViewの後、レイアウトファイルにいくつかのボタンを置いています。CanvasViewでボタンをアンドロイドに表示するにはどうすればいいですか?

これは私のCanvasViewコード、

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.graphics.PorterDuff; 
import android.graphics.drawable.Drawable; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.Toast; 

/** 
* Created by user on 4/28/2016. 
*/ 
public class CanvasView extends View { 

    public int width; 
    public int height; 
    private Bitmap mBitmap; 
    private Canvas mCanvas; 
    private Path mPath; 
    Context context; 
    private Paint mPaint; 
    private float mX, mY; 
    private static final float TOLERANCE = 5; 

    public CanvasView(Context c, AttributeSet attrs) { 
     super(c, attrs); 
     context = c; 

     mPath = new Path(); 

     mPaint = new Paint(); 
     mPaint.setStrokeWidth(3); 
     mPaint.setColor(Color.CYAN); 
    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 

     // your Canvas will draw onto the defined Bitmap 
     mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
     mCanvas = new Canvas(mBitmap); 
    } 

    // override onDraw 
    @Override 
    protected void onDraw(Canvas canvas) { 

     super.onDraw(canvas); 

     Drawable d = getResources().getDrawable(R.drawable.circle_1); 

     int canvasHeight= mCanvas.getHeight(); 
     int canvasWidth= mCanvas.getWidth(); 

     Log.d("Height - ","/"+mCanvas.getHeight()); 
     Log.d("Width - ","/"+mCanvas.getWidth()); 

//   DisplayMetrics displaymetrics = new DisplayMetrics(); 
//  ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
//  int height = displaymetrics.heightPixels; 
//  int width = displaymetrics.widthPixels; 

     float h=canvasHeight/2; 
     float w=canvasWidth/2; 
     float r=canvasWidth/2; 

     d.setBounds(0, 0, canvasWidth, (canvasHeight/8) * 5); 
     d.draw(canvas); 

     // canvas.drawCircle(w, h, r, mPaint); 
    } 

} 

であり、これは、

<?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.co.CanvasView 
     android:id="@+id/signature_canvas" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" /> 

    <LinearLayout 
     android:id="@+id/llParent" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/signature_canvas" 
     android:layout_marginTop="50dp" 
     android:orientation="horizontal"> 

     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="39dp" 
      android:layout_height="wrap_content" 
      android:text="1" 
      android:id="@+id/step18button1" 
      android:textSize="13dp" 
      android:layout_marginLeft="3dp" 
      android:layout_weight="1" 
      android:background="@drawable/button_border" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 

     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="39dp" 
      android:layout_height="wrap_content" 
      android:text="2" 
      android:id="@+id/step18button2" 
      android:textSize="13dp" 
      android:layout_marginLeft="3dp" 
      android:layout_weight="1" 
      android:layout_alignTop="@+id/step18button1" 
      android:layout_toRightOf="@+id/step18button1" 
      android:layout_toEndOf="@+id/step18button1" 
      android:background="@drawable/button_border"/> 

     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="39dp" 
      android:layout_height="wrap_content" 
      android:text="3" 
      android:id="@+id/step18button3" 
      android:textSize="13dp" 
      android:layout_marginLeft="3dp" 
      android:layout_weight="1" 
      android:layout_alignBottom="@+id/step18button2" 
      android:layout_toRightOf="@+id/step18button2" 
      android:layout_toEndOf="@+id/step18button2" 
      android:background="@drawable/button_border"/> 

     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="39dp" 
      android:layout_height="wrap_content" 
      android:text="4" 
      android:id="@+id/step18button4" 
      android:textSize="13dp" 
      android:layout_marginLeft="3dp" 
      android:layout_weight="1" 
      android:layout_alignBottom="@+id/step18button3" 
      android:layout_toRightOf="@+id/step18button3" 
      android:layout_toEndOf="@+id/step18button3" 
      android:background="@drawable/button_border"/> 

     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="39dp" 
      android:layout_height="wrap_content" 
      android:text="5" 
      android:id="@+id/step18button5" 
      android:textSize="13dp" 
      android:layout_marginLeft="3dp" 
      android:layout_weight="1" 
      android:layout_alignBottom="@+id/step18button4" 
      android:layout_toRightOf="@+id/step18button4" 
      android:layout_toEndOf="@+id/step18button4" 
      android:background="@drawable/button_border"/> 

     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="39dp" 
      android:layout_height="wrap_content" 
      android:text="6" 
      android:id="@+id/step18button6" 
      android:textSize="13dp" 
      android:layout_marginLeft="3dp" 
      android:layout_weight="1" 
      android:layout_alignBottom="@+id/step18button5" 
      android:layout_toRightOf="@+id/step18button5" 
      android:layout_toEndOf="@+id/step18button5" 
      android:background="@drawable/button_border"/> 

     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="39dp" 
      android:layout_height="wrap_content" 
      android:text="7" 
      android:id="@+id/step18button7" 
      android:textSize="13dp" 
      android:layout_marginLeft="3dp" 
      android:layout_weight="1" 
      android:layout_alignBottom="@+id/step18button6" 
      android:layout_toRightOf="@+id/step18button6" 
      android:layout_toEndOf="@+id/step18button6" 
      android:background="@drawable/button_border"/> 

     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="39dp" 
      android:layout_height="wrap_content" 
      android:text="8" 
      android:id="@+id/step18button8" 
      android:textSize="13dp" 
      android:layout_marginLeft="3dp" 
      android:layout_weight="1" 
      android:layout_alignBottom="@+id/step18button7" 
      android:layout_toRightOf="@+id/step18button7" 
      android:layout_toEndOf="@+id/step18button7" 
      android:background="@drawable/button_border"/> 

     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="39dp" 
      android:layout_height="wrap_content" 
      android:text="9" 
      android:id="@+id/step18button9" 
      android:textSize="13dp" 
      android:layout_marginLeft="3dp" 
      android:layout_weight="1" 
      android:layout_alignBottom="@+id/step18button8" 
      android:layout_toRightOf="@+id/step18button8" 
      android:layout_toEndOf="@+id/step18button8" 
      android:background="@drawable/button_border"/> 
    </LinearLayout> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="20dp" 
     android:id="@+id/step18editText" 
     android:paddingLeft="10dp" 
     android:layout_below="@+id/llParent" 
     android:layout_alignLeft="@+id/llParent" 
     android:layout_alignStart="@+id/llParent" 
     android:layout_marginTop="20dp" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="20dp" 
     android:id="@+id/step18editText2" 
     android:layout_alignTop="@+id/step18editText" 
     android:layout_centerHorizontal="true" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="20dp" 
     android:id="@+id/step18editText3" 
     android:paddingRight="10dp" 
     android:layout_alignTop="@+id/step18editText2" 
     android:layout_alignRight="@+id/step18textView5" 
     android:layout_alignEnd="@+id/step18textView5" 
     android:layout_marginRight="5dp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/content25" 
     android:id="@+id/step18textView2" 
     android:textAlignment="textStart" 
     android:textSize="18dp" 
     android:paddingLeft="10dp" 
     android:layout_alignTop="@+id/step18textView4" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/content26" 
     android:id="@+id/step18textView4" 
     android:textSize="18dp" 
     android:textAlignment="center" 
     android:layout_alignTop="@+id/step18textView5" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/content27" 
     android:id="@+id/step18textView5" 
     android:textSize="18dp" 
     android:textAlignment="textEnd" 
     android:paddingRight="10dp" 
     android:layout_below="@+id/step18editText3" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 

</RelativeLayout> 

私のレイアウトファイルであり、これは私がCanvasViewでイメージを作成する方法である、

import android.app.Activity; 
import android.content.pm.ActivityInfo; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 

/** 
* Created by user on 5/23/2016. 
*/ 
public class StepTwentyOneFragment extends Fragment { 

    private CanvasView customCanvas; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.step21_fragment, container, false); 
     customCanvas=(CanvasView)v.findViewById(R.id.signature_canvas); 

     return v; 
    } 

    public static StepTwentyOneFragment newInstance() { 

     StepTwentyOneFragment f = new StepTwentyOneFragment(); 
     Bundle b = new Bundle(); 

     f.setArguments(b); 

     return f; 
    } 


    @Override 
    public void setUserVisibleHint(boolean isVisibleToUser) { 
     super.setUserVisibleHint(isVisibleToUser); 
     if(isVisibleToUser) { 
      Activity a = getActivity(); 
      if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
     } 
    } 

} 

ときこのアプリケーションを起動します。CanvasViewの下のボタンは表示されません。今私の質問は、なぜ私はボタンを見ることができませんでしたか?

ボタンを表示するにはどうすればCanvasViewの高さを減らすことができますか?

答えて

1

CanvasViewをandroid:layout_height="match_parent"に、LinearLayoutをandroid:layout_below="@+id/signature_canvas"に設定しています。したがって、CanvasViewは画面全体をカバーし、LinearLayoutは画面の外にあります。

CanvasViewをwrap_contentに変更するか、デザインの必要性に応じて固定高さに変更してください。

1

Canvas Viewandroid:layout_height="match_parent"~android:layout_height="wrap_contentです。またはいくつかの固定HeightCanvas Viewに与える。

これを参照してください。

<com.example.co.CanvasView 
    android:id="@+id/signature_canvas" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" /> 
関連する問題