【问题标题】:Scrollview contains viewpager and one linearlayoutScrollview 包含 viewpager 和一个线性布局
【发布时间】:2017-04-19 17:04:09
【问题描述】:

我尝试使用ViewPager 制作一个可滚动的视图,这将占据屏幕的 90%,然后是ViewPager 中有关图像的一些有用信息。

但是,当我添加 ScrollView 小部件时,即使我设置了 android:fillViewport="true",所有尺寸和重量都不起作用。这是我的布局。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    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="wrap_content"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="100"
        android:scrollbars="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_light"
            android:elevation="0dp"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme"
            app:popupTheme="?android:attr/homeAsUpIndicator" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="90"
            android:orientation="vertical"
            android:padding="50dip">

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

        <TextView
            android:id="@+id/product_sheet_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="bbb"/>

        <TextView
            android:id="@+id/product_sheet_color"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Color: Yellow" />

        <TextView
            android:id="@+id/product_sheet_reference"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Ref: 2049/889/300" />

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

【问题讨论】:

  • 请检查我的答案,让我知道它是否有效。谢谢!
  • 使 NestedScrollView match_parent 并为您的 NestedScrollView 添加一个属性 fitSystemWindows=true
  • @MuhibPirani 它会引发错误:(2) 即使我添加了“compile 'com.android.support:design:25.3.1 '" 在 gradle 脚本中
  • @ReazMurshed 谢谢。但问题是:当我在包装 viewpager 的线性布局之后添加大量视图时,viewpager 会减小其大小。就像nestedscrollview 有一个固定的高度,然后不支持滚动移动。但是,viewpager 必须覆盖 90% 的屏幕。

标签: android android-layout android-fragments android-viewpager android-scrollview


【解决方案1】:

删除android:fillViewport="true" 并将NestedScrollViewlayout_height 设置为match_parent

所以这里的最终布局应该是这样的。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    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:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:scrollbars="vertical"
        android:weightSum="100">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_light"
            android:elevation="0dp"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme"
            app:popupTheme="?android:attr/homeAsUpIndicator" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="90"
            android:orientation="vertical"
            android:padding="50dip">

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

        </LinearLayout>

        <TextView
            android:id="@+id/product_sheet_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="bbb" />

        <TextView
            android:id="@+id/product_sheet_color"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Color: Yellow" />

        <TextView
            android:id="@+id/product_sheet_reference"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Ref: 2049/889/300" />

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

【讨论】:

  • 谢谢。但问题是:当我在包裹 viewpager 的线性布局之后添加大量视图时,viewpager 会减小其大小。
【解决方案2】:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"

    >

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:layout_above="@+id/relativeLayoutBottom"
        >
        <android.support.v4.view.ViewPager
            android:id="@+id/viewPagerPlanDetailBroucher"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:columnWidth="250dp"
            android:numColumns="auto_fit"
            android:verticalSpacing="1dp"
            android:horizontalSpacing="1dp"
            android:layout_marginRight="50dp"
            android:layout_marginLeft="50dp"
            >
        </android.support.v4.view.ViewPager>
        </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayoutBottom"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp"

        android:paddingTop="5dp"
        >
        <TextView
            android:id="@+id/product_sheet_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="bbb"/>

        <TextView
            android:id="@+id/product_sheet_color"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/product_sheet_description"
            android:text="Color: Yellow" />

        <TextView
            android:id="@+id/product_sheet_reference"
            android:layout_below="@+id/product_sheet_color"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="Ref: 2049/889/300" />

    </RelativeLayout>
</RelativeLayout>

【讨论】:

  • 谢谢!当我在 ScrollView 中添加这个良好的布局时,问题仍然存在
  • 不要将其添加到滚动视图中,只需在另一个 XML 文件中进行测试...我已将相对布局与 90 viewpager 分开
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多