2017-05-03 14 views
0

Ich versuche, eine Ansicht zu erstellen, die alle den verfügbaren Platz auf dem Bildschirm füllen. Iv'e stellte meine Ansicht Layout params match_parent und verwendet getHeight() und getWidth() wenn zog Rect auf Canvas und es füllt sich immer noch nur etwa zwei Drittel des Bildschirms.Custom füllt nicht verfügbar Raum

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="match_parent" 
android:layout_width="match_parent" 
xmlns:beat_box="http://schemas.android.com/tools"> 

<com.yotam.aprivate.demo.mybeatbox.Views.BeatBox 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    beat_box:my_color="5D32EA"> 
</com.yotam.aprivate.demo.mybeatbox.Views.BeatBox> 
</RelativeLayout> 

Custom:

public class BeatBox extends View { 
private int color; 
private Context context; 
private Paint paintBox = new Paint(); 
private Paint paintBackground = new Paint(); 

public BeatBox(Context context, @Nullable AttributeSet attrs) { 
    super(context, attrs); 
    this.context = context; 
    color = getAttrColor(attrs); 
    initDrawing(); 
} 
@Override 
protected void onDraw(Canvas canvas) { 
    canvas.drawRect(0,0,canvas.getHeight(), canvas.getWidth(), paintBackground); //this.getHeight() this.getWidth() also doesn't work 
    paintBackground.setShader(new LinearGradient(0, 0, getWidth() , getHeight(), Color.parseColor("#cb356b"), Color.parseColor("#bd3f32"), Shader.TileMode.MIRROR)); 
    super.onDraw(canvas); 
} 

It doesn't fill the whole layout, I want it to fill the white area

Antwort

3

Sie inversed haben die Breite und Höhe in dieser Zeile:

canvas.drawRect(0,0, canvas.getWidth(),canvas.getHeight(), paintBackground); 
1

Parameter der drawRect() Methode sind wie folgt:

Sie haben canvas.getHeight() als right und canvas.getWidth() als bottom verwendet, das ist, warum die height von custom view gleiche ist wie die Vorrichtung width.

LÖSUNG:

Sie canvas.getWidth() als right und canvas.getHeight() als bottom

aktualisieren onDraw() Methode wie unten verwenden sollten:

@Override 
protected void onDraw(Canvas canvas) { 
    canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paintBackground); 
    paintBackground.setShader(new LinearGradient(0, 0, getWidth() , getHeight(), Color.parseColor("#cb356b"), Color.parseColor("#bd3f32"), Shader.TileMode.MIRROR)); 

    super.onDraw(canvas); 
} 

Hope this helfen ~