【问题标题】:Horizontal scroll view with child TextViews not scrolling带有子 TextView 的水平滚动视图不滚动
【发布时间】:2019-01-03 08:35:39
【问题描述】:

我正在向用户展示一些统计数据。屏幕上有很多信息,所以空间很狭窄。我决定使用总共包含 9 个 TextView 的水平滚动视图。但是,我只想一次显示其中三个文本视图。

像这样:

(箭头是一个单独的图像视图,只显示他们可以滚动的用户)

这很好,我希望它看起来如何。但是,我需要显示其他 6 个统计数据。我希望能够向右滚动并出现三个新的文本视图(当我到达最后三个统计数据时回到左边)。这个水平滚动视图是具有 9 个子 TextView 的 LinearLayout 的父级。我尝试将每三个 TextView 放在自己的线性布局中,但由于水平滚动视图只能有一个 LinearLayout 子级而被阻止。

我目前对gone 有其他 6 个统计数据可见性,因为它们目前按垂直顺序添加到前 3 个统计数据下方,如下图所示:

我的计划最初是以编程方式显示接下来的三个统计数据并在滚动时隐藏原始数据,但在我对此进行编程之前,我测试了前三个是否可以滚动,但当我尝试滚动时它们没有反应.

我是怎么知道的?

这是我当前的 xml:

 <HorizontalScrollView
    android:id="@+id/sv_horizontalP1Stats"
    android:layout_width="0dp"
    android:layout_height="97dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    app:layout_constraintBottom_toBottomOf="@+id/sv_horizontalP2Stats"
    app:layout_constraintEnd_toStartOf="@+id/sv_player1PastScores"
    app:layout_constraintHorizontal_bias="0.596"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/iv_navArrowP1">

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

        <TextView
            android:id="@+id/tv_legAvg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="@string/tv_LegAVG"
            android:textSize="13sp" />

        <TextView
            android:id="@+id/tv_matchAVG"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="@string/tv_MatchAVG"
            android:textSize="13sp" />

        <TextView
            android:id="@+id/tv_first6Avg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="@string/tv_first6AVG"
            android:textSize="13sp" />

        <TextView
            android:id="@+id/tv_100plus"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/tv_100plus"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tv_140Plus"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/tv_140plus"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tv_180s"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/tv_180"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tv_bestFinish"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/tv_bestFinish"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tv_bestLeg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/tv_BestLeg"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tv_doublesPercentage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/tv_doubles"
            android:visibility="gone" />


    </LinearLayout>

</HorizontalScrollView>

【问题讨论】:

    标签: android xml


    【解决方案1】:

    您需要做的就是在主线性布局中创建多个线性布局。每个包含 3 个文本视图,并将主要线性布局的方向更改为水平,你就可以开始了。

    例如:

    <HorizontalScrollView
        android:id="@+id/sv_horizontalP1Stats"
        android:layout_width="0dp"
        android:layout_height="97dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintBottom_toBottomOf="@+id/sv_horizontalP2Stats"
        app:layout_constraintEnd_toStartOf="@+id/sv_player1PastScores"
        app:layout_constraintHorizontal_bias="0.596"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/iv_navArrowP1">
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal"> <!-- Changing orientation to horizontal -->
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"> <!-- Group 1 of TV  -->
    
                <TextView
                    android:id="@+id/tv_legAvg"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:text="@string/tv_LegAVG"
                    android:textSize="13sp" />
    
                <TextView
                    android:id="@+id/tv_matchAVG"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:text="@string/tv_MatchAVG"
                    android:textSize="13sp" />
    
                <TextView
                    android:id="@+id/tv_first6Avg"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:text="@string/tv_first6AVG"
                    android:textSize="13sp" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"> <!-- Group 2 -->
    
                // .. Next three
            </LinearLayout>
    
            // .. And soo on
    
        </LinearLayout>
    
    </HorizontalScrollView>`
    

    【讨论】:

    • 我认为您需要为每个内部线性布局应用约束,否则这将导致零宽度布局。
    • 这就是我要找的。我之前尝试过这样的事情,只是我错过了顶部嵌套的LinearLayout之一。并且得到Horizo​​ntalScrollView只能有一个子错误。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多