2017-11-19 5 views
1

私はスクリーンの上部にボタン、中間にキャンバス、そしてスクリーンの下部にいくつかのボタンを持つスクリーンを設計しています。ここに私の現在のレイアウトがあります:2つのウィジェットの間にキャンバスを表示するにはどうすればいいですか?

私の主な活動で
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <Button 
     android:text="Button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/button1" /> 
    <View 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/iv_view1" 
     android:layout_marginBottom="0.0dp" /> 
    <Button 
     android:text="Button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/button2" /> 
</LinearLayout> 

、私はこれを試してみました:予想通り

protected override void OnCreate(Bundle savedInstanceState) 
{ 
    base.OnCreate(savedInstanceState); 
    SetContentView(Resource.Layout.Main); 
    var view = FindViewById<View>(Resource.Id.iv_view1); 
    var canvasView = new MyCanvas(this.BaseContext); 
    view = canvasView; 
} 

しかし、結果はありません。一番上のボタンだけが表示され、キャンバスに描画する画像は表示されません。問題はキャンバスではありません。なぜなら、私はそれをフルスクリーンビューとして単独でテストし、うまく動作しているからです。ここに私のキャンバスのコードは、念のためです:

public class MyCanvas : View 
{ 
    private readonly ShapeDrawable _shape; 

    public MyCanvas(Context context) : base(context) 
    { 
     var paint = new Paint(); 
     paint.SetARGB(255, 200, 255, 0); 
     paint.SetStyle(Paint.Style.Stroke); 
     paint.StrokeWidth = 4; 

     _shape = new ShapeDrawable(new OvalShape()); 
     _shape.Paint.Set(paint); 

     _shape.SetBounds(20, 20, 300, 200); 
    } 

    protected override void OnDraw(Canvas canvas) 
    { 
     _shape.Draw(canvas); 
    } 
} 

答えて

1

は、このようなあなたのMyCanvasを変更します。

public class MyCanvas : View 
{ 
    private ShapeDrawable _shape; 

    public MyCanvas(Context context) : this (context, null) 
    { 
    } 

    public MyCanvas(Context context, IAttributeSet attrs) : this (context, attrs, 0) 
    { 
    } 

    public MyCanvas(Context context, IAttributeSet attrs, int defStyle) : base (context, attrs, defStyle) 
    { 
     Init(); 
    } 

    public void Init() 
    { 
     var paint = new Paint(); 
     paint.SetARGB(255, 200, 255, 0); 
     paint.SetStyle(Paint.Style.Stroke); 
     paint.StrokeWidth = 4; 

     _shape = new ShapeDrawable(new OvalShape()); 
     _shape.Paint.Set(paint); 

     _shape.SetBounds(20, 20, 300, 200); 
    } 

    protected override void OnDraw(Canvas canvas) 
    { 
     _shape.Draw(canvas); 
    } 
} 

その後、あなたは例えば、一般的な見解のようなあなたのaxmlファイルでそれを使用することができます。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <Button 
     android:text="Button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/button1" /> 

    <!-- Replace Your_MyCanvas_Namespace with the namespace of your MyCanvas class --> 
    <Your_MyCanvas_Namespace.MyCanvas 
     android:layout_width="wrap_content" 
     android:layout_height="100dp" 
     android:id="@+id/iv_view1" 
     /> 

    <Button 
     android:text="Button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/button2" /> 
</LinearLayout> 

あなたのもの:MainActivity

MyCanvas myCanvas = FindViewById<MyCanvas>(Resource.Id.iv_view1); 

効果:

​​

関連する問題