Android中的match_parent属性未按预期工作

3
在我的布局文件中,我使用了根ScrollView和子LinearLayout
我搜索了一下,但没有得到帮助。我在父视图和子视图中都设置了match_parent
但是子视图存在问题。
子视图未填充父视图。
这里是图片和xml代码。

enter image description here

布局文件。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:CustomTextView="http://schemas.android.com/apk/res-auto"
    xmlns:CustomEditTextView="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#D3D6DB"
    android:orientation="vertical"
    tools:context=".tabs.MyTravelTab" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/card_background_new"
        android:orientation="vertical" >

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp" >

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="@dimen/field_height"
                    android:layout_weight="0.4"
                    android:gravity="left|center_vertical" >

                    <com.mymv.controls.CustomTextView
                        android:id="@+id/textGPS"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        CustomTextView:typeface="Roboto-Regular.ttf"
                        android:text="ABC"
                        android:textColor="@color/black"
                        android:textSize="12sp" />

                    <ToggleButton
                        android:id="@+id/gps_toggle"
                        android:layout_width="wrap_content"
                        android:layout_height="24dp"
                        android:layout_margin="5dp"
                        android:background="@drawable/gps_custom_toggle"
                        android:checked="false"
                        android:textOff=""
                        android:textOn="" />

                    <com.mymv.controls.CustomTextView
                        android:id="@+id/textManual"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        CustomTextView:typeface="Roboto-Regular.ttf"
                        android:text="DEF"
                        android:textColor="@color/black"
                        android:textSize="11sp" />
                </LinearLayout>

                <Spinner
                    android:id="@+id/spinnerPurposeOfJourney"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/field_height"
                    android:layout_weight="0.6"
                    android:background="@drawable/dropdown_black_border"
                    android:entries="@array/purpose_of_journey" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/gpsLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:visibility="gone" >

                <LinearLayout
                    android:id="@+id/startGPSBtn"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/field_height"
                    android:layout_weight="0.5"
                    android:background="@drawable/textview_green_drawable_left_curve_selector"
                    android:gravity="center" >

                    <ImageView
                        android:id="@+id/imageView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/gps" />

                    <com.mymv.controls.CustomTextView
                        android:id="@+id/textView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        CustomTextView:typeface="Roboto-Regular.ttf"
                        android:text="START GPS"
                        android:textColor="@color/white" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/endGPSBtn"
                    android:layout_width="0dp"
                    android:layout_height="@dimen/field_height"
                    android:layout_weight="0.5"
                    android:background="@drawable/textview_red_drawable_right_curve_selector"
                    android:gravity="center" >

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/end_flag" />

                    <com.mymv.controls.CustomTextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        CustomTextView:typeface="Roboto-Regular.ttf"
                        android:text="END GPS"
                        android:textColor="@color/white" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="10dp" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center" >

                    <com.mymv.controls.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="@dimen/field_height"
                        android:layout_weight="@integer/layout_weight_textview"
                        CustomTextView:typeface="Roboto-Bold.ttf"
                        android:background="@drawable/textview_gray_drawable"
                        android:gravity="center_vertical"
                        android:padding="5dp"
                        android:text="DATA"
                        android:textColor="@color/gray" />

                    <com.mymv.controls.CustomTextView
                        android:id="@+id/edtTxtDate"
                        android:layout_width="0dp"
                        android:layout_height="@dimen/field_height"
                        android:layout_marginLeft="-1dp"
                        android:layout_weight="@integer/layout_weight_edittext"
                        CustomTextView:typeface="Roboto-Regular.ttf"
                        android:background="@drawable/edittext_drawable_right_curv_selector"
                        android:gravity="center_vertical"
                        android:hint="DATA"
                        android:padding="5dp"
                        android:singleLine="true"
                        android:textColor="@color/black"
                        android:textColorHint="@color/gray"
                        android:textSize="15dp" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/manualLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:visibility="visible" >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:gravity="center" >

                        <com.mymv.controls.CustomTextView
                            android:layout_width="0dp"
                            android:layout_height="@dimen/field_height"
                            android:layout_weight="@integer/layout_weight_textview"
                            CustomTextView:typeface="Roboto-Bold.ttf"
                            android:background="@drawable/textview_gray_drawable"
                            android:gravity="center_vertical"
                            android:padding="5dp"
                            android:text="DATA"
                            android:textColor="@color/gray" />

                        <com.mymv.controls.CustomEditTextView
                            android:id="@+id/edtTxtStartOdometer"
                            android:layout_width="0dp"
                            android:layout_height="@dimen/field_height"
                            android:layout_marginLeft="-1dp"
                            android:layout_weight="@integer/layout_weight_edittext"
                            CustomTextView:typeface="Roboto-Regular.ttf"
                            android:background="@drawable/edittext_drawable_right_curv_selector"
                            android:focusable="false"
                            android:gravity="center_vertical"
                            android:hint="DATA"
                            android:inputType="number"
                            android:maxLength="6"
                            android:padding="5dp"
                            android:singleLine="true"
                            android:textColor="@color/black"
                            android:textColorHint="@color/gray"
                            android:textSize="15dp" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:gravity="center" >

                        <com.mymv.controls.CustomTextView
                            android:layout_width="0dp"
                            android:layout_height="@dimen/field_height"
                            android:layout_weight="@integer/layout_weight_textview"
                            CustomTextView:typeface="Roboto-Bold.ttf"
                            android:background="@drawable/textview_gray_drawable"
                            android:gravity="center_vertical"
                            android:padding="5dp"
                            android:text="DATA"
                            android:textColor="@color/gray" />

                        <com.mymv.controls.CustomEditTextView
                            android:id="@+id/edtTxtEndOdometer"
                            android:layout_width="0dp"
                            android:layout_height="@dimen/field_height"
                            android:layout_marginLeft="-1dp"
                            android:layout_weight="@integer/layout_weight_edittext"
                            CustomEditTextView:typeface="Roboto-Regular.ttf"
                            android:background="@drawable/edittext_drawable_right_curv_selector"
                            android:focusable="false"
                            android:gravity="center_vertical"
                            android:hint="DATA"
                            android:inputType="number"
                            android:maxLength="6"
                            android:padding="5dp"
                            android:singleLine="true"
                            android:textColor="@color/black"
                            android:textColorHint="@color/gray"
                            android:textSize="15dp" />
                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:gravity="center" >

                    <com.mymv.controls.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="@dimen/field_height"
                        android:layout_weight="@integer/layout_weight_textview"
                        CustomTextView:typeface="Roboto-Bold.ttf"
                        android:background="@drawable/textview_gray_drawable"
                        android:gravity="center_vertical"
                        android:padding="5dp"
                        android:text="DATA"
                        android:textColor="@color/gray" />

                    <com.mymv.controls.CustomEditTextView
                        android:id="@+id/edtTxtStartLocation"
                        android:layout_width="0dp"
                        android:layout_height="@dimen/field_height"
                        android:layout_marginLeft="-1dp"
                        android:layout_weight="@integer/layout_weight_edittext"
                        CustomEditTextView:typeface="Roboto-Regular.ttf"
                        android:background="@drawable/edittext_drawable_right_curv_selector"
                        android:focusable="false"
                        android:gravity="center_vertical"
                        android:hint="DATA"
                        android:maxLength="100"
                        android:padding="5dp"
                        android:singleLine="true"
                        android:textColor="@color/black"
                        android:textColorHint="@color/gray"
                        android:textSize="15dp" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:gravity="center" >

                    <com.mymv.controls.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="@dimen/field_height"
                        android:layout_weight="@integer/layout_weight_textview"
                        CustomTextView:typeface="Roboto-Bold.ttf"
                        android:background="@drawable/textview_gray_drawable"
                        android:gravity="center_vertical"
                        android:padding="5dp"
                        android:text="DATA"
                        android:textColor="@color/gray" />

                    <com.mymv.controls.CustomEditTextView
                        android:id="@+id/edtTxtEndLocation"
                        android:layout_width="0dp"
                        android:layout_height="@dimen/field_height"
                        android:layout_marginLeft="-1dp"
                        android:layout_weight="@integer/layout_weight_edittext"
                        CustomEditTextView:typeface="Roboto-Regular.ttf"
                        android:background="@drawable/edittext_drawable_right_curv_selector"
                        android:focusable="false"
                        android:gravity="center_vertical"
                        android:hint="DATA"
                        android:maxLength="100"
                        android:padding="5dp"
                        android:singleLine="true"
                        android:textColor="@color/black"
                        android:textColorHint="@color/gray"
                        android:textSize="15dp" />
                </LinearLayout>

                <Button
                    android:id="@+id/btnRecord"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/field_height"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/button_blue_border"
                    android:text="Button"
                    android:textColor="@color/white"
                    android:textSize="15dp" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/viewUsage"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="0.5"
                android:background="@drawable/button_background_yellow"
                android:padding="10dp"
                android:text="Button1"
                android:textColor="@color/black" />

            <Button
                android:id="@+id/viewTravelHistory"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="0.5"
                android:background="@drawable/button_background_blue"
                android:padding="10dp"
                android:text="Button2"
                android:textColor="@color/white" />
        </LinearLayout>
    </LinearLayout>

</ScrollView>

1
ScrollView 的作用是什么? - Abdul Fatir
1
你在那里期望了什么? - Tim
1
它没有填满整个空间,因为滚动视图。它只会填充所需的空间。因此,请尝试使用fill_parent或在另一个布局中使用滚动视图。 - Sagar Nayak
你尝试过在ScrollView中使用fillViewport:true吗? - Sumighosh Charuvil
但在我的情况下是必要的。 - Chirag Savsani
显示剩余4条评论
3个回答

8
尝试在您的滚动视图中使用android:fillViewport="true"

它使用 android:fillViewport="true" 工作正常,但活动页面无法滚动。 - Chirag Savsani
由于您的内容较少,一旦您的内容超过了区域大小,它将开始滚动。请接受最佳答案 :) - Neo
没有内容就太少了。当键盘打开时,它不会滚动。我在小设备上测试过,但内容无法滚动。 - Chirag Savsani
我也在安卓清单文件中添加了 android:windowSoftInputMode="adjustResize" 这行代码。 - Chirag Savsani

1

建议在ScrollView中使用wrap_content来设置LinearLayout的大小。

为了显示完整的列表,您可以:

  1. 使用ScrollView的android:fillViewport="true"属性

或者

  1. 为LinearLayout使用确切的宽度和高度值。您可以根据需要计算它,例如与屏幕尺寸相等,或与其他视图大小相等...

它在使用android:fillViewport="true"时工作正常,但活动页面无法滚动。 - Chirag Savsani
那就试试第二种方法吧 :) 你可以将linearLayout的高度设置得比scrollView大一点,而不会影响性能。 - Alex Hong


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接