Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使自定义视图使用布局权重_Java_Android_Android Custom View - Fatal编程技术网

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

Java 使自定义视图使用布局权重,java,android,android-custom-view,Java,Android,Android Custom View,我正在尝试制作一个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_h

我正在尝试制作一个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是文本视图。我已经设法让文本视图在垂直模式下使用一个线性布局,其子布局的权重设置为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文件构建的,我尝试过处理布局宽度、布局高度、布局重量、布局重力,但似乎没有任何东西可以解决这个问题。如果您有任何想法,我们将不胜感激。

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

您可以包括您的实际XML布局吗?@HenryTwist使用XML编辑谢谢,这已经困扰了我两个小时:)
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
    }
}