繁体   English   中英

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

[英]Make custom view use layout weight

我正在尝试制作具有以下布局的 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
    }
}

class 是从 XML 文件构建的,我试过弄乱 layout_width、layout_height、layout_weight、layout_gravity,但似乎没有什么能解决它。 将不胜感激任何想法

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM