【问题标题】:layout_weight is not working inside ScrollViewlayout_weight 在 ScrollView 中不起作用
【发布时间】:2018-11-12 19:00:48
【问题描述】:

我想不通,为什么 layout_weight 在 ScrollView 中不起作用,当我运行这个 xml 时,我正在获取 imageview 捕获整个屏幕,并且在下面我可以看到我的选项卡并且 viewpager 被隐藏了。

当我将 dp 中的高度赋予 ScrollView 下方的所有线性布局时,我得到了完美的视图,为什么会发生这种情况。我希望我的 imageview 占据 30% 的屏幕,而 tab/viewpager 占据 70% 的屏幕。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.emilsjolander.components.StickyScrollViewItems.StickyScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
            <LinearLayout
                android:id="@+id/firstLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:weightSum="10">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:background="@color/gift_vouchr_header_img"
                    android:layout_weight="3"
                    android:adjustViewBounds="true"
                    android:layout_gravity="center">
                    <ImageView
                        android:id="@+id/imageview_gift"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/splash"/>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/viewpager_ll"
                    android:layout_width="match_parent"
                    android:background="@color/lvb_divider_color"
                    android:layout_height="0dp"
                    android:orientation="vertical"
                    android:tag="sticky"
                    android:layout_weight="7"
                    android:adjustViewBounds="true"
                    android:layout_gravity="center"
                    >
                    <android.support.design.widget.TabLayout
                        android:id="@+id/common_tablayout"
                        style="@style/VoucherTabLayoutStyle"
                        android:layout_width="match_parent"
                        app:tabGravity="fill"
                        android:layout_height="@dimen/margin_48">
                    </android.support.design.widget.TabLayout>


                    <android.support.v4.view.ViewPager
                        android:id="@+id/common_viewpager"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                    </android.support.v4.view.ViewPager>
                </LinearLayout>
            </LinearLayout>
    </com.emilsjolander.components.StickyScrollViewItems.StickyScrollView>
</LinearLayout>

当我删除 weightsum 并对高度进行硬编码时,视图工作正常:下面是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.emilsjolander.components.StickyScrollViewItems.StickyScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
            <LinearLayout
                android:id="@+id/firstLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="160dp"
                    android:background="@color/gift_vouchr_header_img"
                    android:adjustViewBounds="true"
                    android:layout_gravity="center">
                    <ImageView
                        android:id="@+id/imageview_gift"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/splash"/>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/viewpager_ll"
                    android:layout_width="match_parent"
                    android:background="@color/lvb_divider_color"
                    android:layout_height="500dp"
                    android:orientation="vertical"
                    android:tag="sticky"
                    android:adjustViewBounds="true"
                    android:layout_gravity="center">

                    <android.support.design.widget.TabLayout
                        android:id="@+id/common_tablayout"
                        style="@style/VoucherTabLayoutStyle"
                        android:layout_width="match_parent"
                        app:tabGravity="fill"
                        android:layout_height="@dimen/margin_48">
                    </android.support.design.widget.TabLayout>


                    <android.support.v4.view.ViewPager
                        android:id="@+id/common_viewpager"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                    </android.support.v4.view.ViewPager>
                </LinearLayout>

            </LinearLayout>
    </com.emilsjolander.components.StickyScrollViewItems.StickyScrollView>
</LinearLayout>

MyTab1 xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/margin_15"
        android:layout_marginRight="@dimen/margin_15"
        android:layout_marginTop="@dimen/margin_15"
        android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/gift_amount_footer_txt"
        android:text="@string/validity"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <include layout="@layout/voucher_common_layout"/>

    </LinearLayout>


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/gift_title_txt"
            android:layout_marginTop="@dimen/margin_26"
            android:text="@string/receiver_detail"/>
        <EditText
            android:id="@+id/receiver_name"
            android:layout_width="match_parent"
            android:layout_height="@dimen/margin_48"
            android:backgroundTint="@color/QuaternaryTextColor"
            android:hint="@string/receiver_name"
            android:paddingLeft="@dimen/margin_2"
            android:layout_marginTop="@dimen/margin_23"/>
        <EditText
            android:id="@+id/receiver_email"
            android:layout_width="match_parent"
            android:layout_height="@dimen/margin_48"
            android:hint="@string/receiver_email"
            android:inputType="textEmailAddress"
            android:backgroundTint="@color/QuaternaryTextColor"

            style="@style/giftEditetxtStyle"
            android:paddingLeft="@dimen/margin_2"
            android:layout_marginTop="@dimen/margin_30"/>
        <EditText
            android:id="@+id/receiver_mobile"
            android:layout_width="match_parent"
            android:layout_height="@dimen/margin_48"
            android:backgroundTint="@color/QuaternaryTextColor"
            android:inputType="phone"
            android:hint="@string/receiver_mobile"
            android:paddingLeft="@dimen/margin_2"
            android:layout_marginTop="@dimen/margin_30"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_40"
            android:layout_marginBottom="@dimen/margin_30"
            android:layout_marginRight="@dimen/margin_12"
            android:gravity="center"
            android:orientation="horizontal">
            <CheckBox
                android:id="@+id/send_cb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/gift_term_condition"
                android:layout_marginLeft="@dimen/margin_5"
                android:text="@string/gift_terms_condition"/>
        </LinearLayout>

        <Button
            android:id="@+id/btn_send_gift"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/gift_submit_btn"
            android:layout_marginBottom="@dimen/margin_10"
            android:layout_marginTop="@dimen/margin_20"/>
    </LinearLayout>

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

MyTab2 xml:

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="@dimen/margin_15"
        android:layout_marginLeft="@dimen/margin_15"
        android:layout_marginRight="@dimen/margin_15">

    <include layout="@layout/voucher_common_layout"/>

        <LinearLayout
            android:id="@+id/terms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_40"
            android:layout_marginRight="@dimen/margin_12"
            android:orientation="horizontal"
            android:gravity="center">
            <CheckBox
                android:id="@+id/buy_cb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"  />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/gift_term_condition"
                android:layout_marginLeft="@dimen/margin_5"
                android:text="@string/gift_terms_condition"/>
        </LinearLayout>
        <Button
            android:id="@+id/gift_buy_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/gift_submit_btn"
            android:layout_marginTop="@dimen/margin_30"/>
    </LinearLayout>


</LinearLayout>
</android.support.v4.widget.NestedScrollView>

【问题讨论】:

  • 我猜你想要页面底部附近的“提交”按钮,靠近导航。如果是这样的话,你应该把它写成最后一句话。我的猜测是您应该使用不同类型的布局,但我只是在这里猜测。
  • 你想要什么?您的按钮将显示在下方。
  • 是的,我最后需要提交按钮,但我已将该按钮放在片段中,我不需要在主布局中使用该按钮,因为两个选项卡具有不同类型的按钮形状和大小,它是布局本身的按钮,但是当我滚动它时,它会继续滚动并留下额外的空间
  • @farhana 有两个选项卡,每个选项卡都有自己的布局,在每个布局中都有最后包含按钮的视图,布局很完美,但是当我向上滚动时,按钮会向上,当用户滚动时我需要什么,它只显示相同的高度,但它显示了额外的高度,我将我的 xml 放入视图代码中
  • 将您的按钮保持在ScrollView 之外并在 ScrollView` 之外获取另一个视图,这会将两个视图放置在它们的位置,然后您的按钮将不会随着内容滚动。

标签: android android-layout android-scrollview


【解决方案1】:

上述解决方案都不适合我,我自己找到了这样的解决方案:希望它对某人有所帮助:标签代码没有变化。在主 xml 中,我这样做了:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.emilsjolander.components.StickyScrollViewItems.StickyScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:layout_gravity="center">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/childImg_rl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/gift_vouchr_header_img"
                android:adjustViewBounds="true"
                android:layout_gravity="center">
                <ImageView
                    android:id="@+id/imageview_gift"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/splash"/>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/viewpager_ll"
                android:layout_width="match_parent"
                android:background="@color/lvb_divider_color"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:tag="sticky"
                android:adjustViewBounds="true"
                android:layout_gravity="center">

                <android.support.design.widget.TabLayout
                    android:id="@+id/common_tablayout"
                    style="@style/VoucherTabLayoutStyle"
                    android:layout_width="match_parent"
                    app:tabGravity="fill"
                    app:tabMode="fixed"
                    android:layout_height="@dimen/margin_48">
                </android.support.design.widget.TabLayout>


                <android.support.v4.view.ViewPager
                    android:id="@+id/common_viewpager"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                </android.support.v4.view.ViewPager>
            </LinearLayout>

        </LinearLayout>
    </com.emilsjolander.components.StickyScrollViewItems.StickyScrollView>
</LinearLayout>

重要的部分来了:务实地设置子布局的高度:D

private void setViewPagerandImageViewHeight(){
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int heightOfScreen = displayMetrics.heightPixels;
        child_img_ll.getLayoutParams().height= (int) (heightOfScreen*.30);// set imageview linearlayout to capture 30% of screen
        viewpager_ll.getLayoutParams().height=heightOfScreen;//while scrolling set the viewpager height to maximum ,intially it capture 70% of screen

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多