使自定义视图使用布局权重

问题描述

我正在尝试制作一个具有以下布局的 Android 应用

<?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>

应该这样布置

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

其中 c 是我的自定义视图,t 是文本视图。我设法使用垂直模式下的一种线性布局将文本视图置于网格中,其子级的 layout_weights 设置为 1,然后水平模式下的子级线性布局和布局权重为 1 的子文本视图。但出于某种原因,我即使设置了权重,似乎也无法让我的自定义视图共享半个线性布局。我的屏幕目前看起来更像:

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

我似乎没有做任何事情使这个视图占据一半的高度。我的观点看起来像:

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
    }
}

这个类是从一个 XML 文件中构造出来的,我试过弄乱 layout_width、layout_height、layout_weight、layout_gravity,但似乎没有什么能解决它。希望有任何想法

解决方法

在使用权重时,您应该使用 0dp 的高度/宽度。使用 wrap_content 有时会导致意外结果。