Search code examples
javaandroidandroid-custom-view

Make custom view use layout weight


I'm trying to make an android app which has the following layout

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

    <com.example.CustomView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:id="@+id/customView" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:weightSum="2"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/TopLeftText"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center" />

            <TextView
                android:id="@+id/TopRightText"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:weightSum="2"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/BottomLeftText"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center" />

            <TextView
                android:id="@+id/BottomRightText"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

and should be laid out like so

+---+---+
|       |
|       |
|       |
|   c   |
|       |
|       |
|       |
+---+---+
|   |   |
| t | t |
|   |   |
+---+---+
|   |   |
| t | t |
|   |   |
+---+---+

where c is my custom view, and t are the textviews. I have managed to get the text views to be in the grid using one linear layout in vertical mode with their children's layout_weights set to 1 and then the child linear layouts in horizontal mode and their child textviews with layout weight 1. But for some reason I can't seem to get my custom view to share half a linear layout even with the weight set. My screen currently looks more like:

+---+---+
|       |
|       |
|       |
|       |
|       |
|   c   |
|       |
|       |
|       |
|       |
|       |
|       |
|       |
+---+---+
| t | t |
+---+---+
| t | t |
+---+---+

Nothing I seem to do makes this view take half the height. My view looks something along the lines of:

class CustomView extends View {
    public CustomView(Context context) {
        super(context);
    }

    public CustomView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    protected void onDraw(Canvas canvas) {
        // here I just have some code to fill the view in with solid colour
    }
}

The class gets constructed from an XML file and I've tried messing with layout_width, layout_height, layout_weight, layout_gravity but nothing seems to fix it. Would appreciate any ideas


Solution

  • You should be using a height/width of 0dp when using weights. Using wrap_content sometimes causes unexpected results.