首页 > 解决方案 > 孩子的Android match_parent没有达到父母的体重

问题描述

就像标题一样。布局在 Android Studio 上看起来很棒,但是当我在真实设备上得到它时,它看起来很糟糕。JavaCameraView 只占屏幕的一半,而在 Android Studio 中,它应该占大约 3/4。JavaCameraView 应该达到屏幕上显示的水平条。

这是我的截图。

在 Android Studio 上:

在此处输入图像描述

在真实设备上:

在此处输入图像描述

Layout Inspector 显示托管 JavaCameraView 的这个 RelativeLayout 占据了所有空间,但具有 height:match_parent 的 JavaCameraView 的行为不是这样:

在此处输入图像描述

为什么呢?

这是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:opencv="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="5"
    android:layout_above="@+id/scroll_view">


    <org.opencv.android.JavaCameraView
        android:id="@+id/java_surface_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        opencv:camera_id="any"
        opencv:show_fps="true" />

</RelativeLayout>

<ScrollView
    android:id="@+id/scroll_view"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_marginBottom="?actionBarSize">

    <RelativeLayout
        android:id="@+id/rl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <View
            android:id="@+id/View1"
            android:layout_width="wrap_content"
            android:layout_height="2dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:background="@android:color/darker_gray" />

        <TextView
            android:id="@+id/alert_pxcm_too_small"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/View1"
            android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            android:textColor="#FF0000"
            android:textSize="@dimen/text_size_rest"
            android:visibility="visible" />

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:layout_below="@id/alert_pxcm_too_small"/>
        <TextView
            android:id="@+id/measurements_width_converter"
            android:layout_marginTop="15dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/spinner"
            android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            android:textColor="#0000FF"
            android:text="@string/width_info"
            android:textSize="@dimen/text_size_rest"
            android:visibility="visible" />

        <TextView
            android:id="@+id/measurements_height_converter"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/measurements_width_converter"
            android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            android:textColor="#0000FF"
            android:text="@string/width_info"
            android:textSize="@dimen/text_size_rest"
            android:visibility="visible" />

    </RelativeLayout>
</ScrollView>

如果您需要更多内容,请询问。可能您还需要一些 Java 类代码、此片段等。提前谢谢您!:)

标签: androidandroid-layout

解决方案


您的问题在于您使用的值android:layout_weight以及如何设置android:layout_widthand android:layout_height

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  ...
  >
  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.3"
   ..
   >
   <org.opencv.android.JavaCameraView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
    ..
    />
  </RelativeLayout>

  <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.7"

    <!-- Other views here -->

  </ScrollView>
</LinearLayout>

推荐阅读