【问题标题】:Unable to make child layout fill its parent inside ScrollView无法使子布局在 ScrollView 中填充其父级
【发布时间】:2017-07-27 12:11:45
【问题描述】:

我在ScrollView 内有一个父布局RelativeLayout,它包含几个子布局和一个子布局(即preview_wrapper)在RelativeLayout 的末尾,它用match_parent LayoutParam 填充它并包含一个背景使用 alpha 颜色,并且其 clickablefocusable 设置为 true,因此父布局内的任何视图都不可点击。但它对我不起作用我无法看到最后一个孩子填充父布局带到前面。但是,当我删除 ScrollView 时,它的工作完美.. 但是我需要将它带入 ScrollView,因为我在 ScrollView 之外制作 preview_wrapper 我无法滚动布局。

结构:

<ScrollView>
    <RelativeLayout>
        <LinearLayout>
        </LinearLayout>

        <!-- the layout I want to bring in front of above linear layout and cover its parent -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/alpha_white"
            android:clickable="true"
            android:focusable="true"
            android:id="@+id/preview_wrapper"
            ></RelativeLayout>


    </RelativeLayout>

</ScrollView>

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@color/white"
    >

  <ScrollView
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="4"
            android:layout_below="@+id/car_pager"
            android:orientation="horizontal"
            android:id="@+id/action_wrap"
            >
            <RelativeLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:id="@+id/action_save"
                android:background="@drawable/btn_action_selector"
                android:clickable="true"
                android:focusable="true"
                android:paddingTop="20dp"
                android:paddingBottom="20dp"

                >
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:orientation="vertical"
                    >
                    <ImageView
                        android:layout_width="15dp"
                        android:layout_height="15dp"
                        android:src="@drawable/ic_star_hollow"

                        android:tint="@color/white"
                        android:id="@+id/ic_save"
                        android:layout_gravity="center"
                        />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="12sp"
                        android:textColor="@color/white"
                        android:text="@string/save"
                        android:textAllCaps="false"
                        android:layout_marginTop="10dp"

                        />
                </LinearLayout>



            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:id="@+id/action_email"
                android:layout_weight="1"
                android:background="@drawable/btn_action_selector"
                android:clickable="true"
                android:focusable="true"
                android:paddingTop="20dp"
                android:paddingBottom="20dp"
                >
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:orientation="vertical"
                    >

                    <ImageView
                        android:layout_width="15dp"
                        android:layout_height="15dp"
                        android:src="@drawable/ic_email_action"
                        android:layout_centerInParent="true"
                        android:tint="@color/white"
                        android:layout_gravity="center"
                        />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="12sp"
                        android:textColor="@color/white"
                        android:text="@string/contact"
                        android:textAllCaps="false"
                        android:layout_marginTop="10dp"
                        />


                </LinearLayout>

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:id="@+id/action_price_alert"
                android:layout_weight="1"
                android:background="@drawable/btn_action_selector"
                android:clickable="true"
                android:focusable="true"
                android:paddingTop="20dp"
                android:paddingBottom="20dp"
                >
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:orientation="vertical"
                    >

                    <ImageView
                        android:layout_width="15dp"
                        android:layout_height="15dp"
                        android:src="@drawable/ic_electric"
                        android:layout_centerInParent="true"
                        android:tint="@color/white"
                        android:id="@+id/price_alt_btn"
                        android:layout_gravity="center"
                        />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="12sp"
                        android:textColor="@color/white"
                        android:text="@string/price_alert"
                        android:textAllCaps="false"
                        android:layout_marginTop="10dp"
                        />

                </LinearLayout>

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:id="@+id/action_flag"
                android:layout_weight="1"
                android:background="@drawable/btn_action_selector"
                android:clickable="true"
                android:focusable="true"
                android:paddingTop="20dp"
                android:paddingBottom="20dp"

                >

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:orientation="vertical"
                    >

                    <ImageView
                        android:layout_width="15dp"
                        android:layout_height="15dp"
                        android:src="@drawable/ic_flag_hollow"
                        android:layout_centerInParent="true"
                        android:tint="@color/white"
                        android:layout_gravity="center"
                        />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="12sp"
                        android:textColor="@color/white"
                        android:text="@string/abuse_report"

                        android:layout_marginTop="10dp"
                        android:textAllCaps="false"
                        />

                </LinearLayout>
            </RelativeLayout>

        </LinearLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/alpha_white"
            android:clickable="true"
            android:focusable="true"
            android:id="@+id/preview_wrapper"
            ></RelativeLayout>

    </RelativeLayout>


    </ScrollView>

</RelativeLayout>

【问题讨论】:

    标签: android android-layout scrollview


    【解决方案1】:

    然后将RelativeLayout 变成FrameLayout 并重新排列子布局,如下所示

    <ScrollView>
        <FrameLayout>
    
            <!-- the layout I want to bring in front of above linear layout and cover its parent -->
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/alpha_white"
                android:clickable="true"
                android:focusable="true"
                android:id="@+id/preview_wrapper"
                ></RelativeLayout>
    
            <LinearLayout>
            </LinearLayout>
        </FrameLayout>
    
    </ScrollView>
    

    【讨论】:

      猜你喜欢
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 2021-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-14
      相关资源
      最近更新 更多