'Custom view in layout doesn't show while using "match_parent"
I have a constraint layout that consists of two TextView
s and a custom BoxBackgroundView
. I want the box background to scale in size with the text, but when I use "match_parent"
on the background and "wrap_content"
on the constraint layout, the background doesn't show. I'm guessing that it just has a height of zero, but I don't know how to fix that.
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<me.fishy.testapp.app.views.BoxBackgroundView
android:id="@+id/boxBackgroundView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:color="@color/box_background_green"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/titleBox"
android:layout_width="256dp"
android:layout_height="wrap_content"
android:text="Filler title"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="40sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
custom:layout_constraintStart_toStartOf="parent"
custom:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textBox"
android:layout_width="256dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Filler text"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
custom:layout_constraintStart_toStartOf="parent"
custom:layout_constraintTop_toBottomOf="@+id/titleBox" />
</androidx.constraintlayout.widget.ConstraintLayout>
BoxBackgroundView:
package me.fishy.testapp.app.views;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import me.fishy.testapp.R;
public class BoxBackgroundView extends View {
private int mExampleColor = 0xFF0000;
private RectF rectangle;
private Paint rectColor;
public BoxBackgroundView(Context context) {
super(context);
init(null, 0);
}
public BoxBackgroundView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}
public BoxBackgroundView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs, defStyle);
}
private void init(AttributeSet attrs, int defStyle) {
// Load attributes
final TypedArray a = getContext().obtainStyledAttributes(
attrs, R.styleable.BoxBackgroundView, defStyle, 0);
mExampleColor = a.getColor(
R.styleable.BoxBackgroundView_color,
mExampleColor);
rectColor = new Paint();
rectColor.setColor(mExampleColor);
a.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
if (rectangle == null){
rectangle = new RectF(0, 0, getWidth(), getHeight());
}
canvas.drawRoundRect(rectangle, 50f, 50.f, rectColor);
}
public int getExampleColor() {
return mExampleColor;
}
public void setExampleColor(int exampleColor) {
mExampleColor = exampleColor;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|