TableLayout 滚动视图 垂直和水平

5
我们如何设置ScrollView垂直和水平滚动?我尝试了下面的代码,但它没有起作用。
<ScrollView
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:background="@color/red"
 android:scrollbarFadeDuration="1000"
 android:scrollbarSize="12dip" >

   <HorizontalScrollView
     android:id="@+id/horizontalScrollView1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" >

      <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:collapseColumns="2"
        android:stretchColumns="1" >
    </TableLayout>
</HorizontalScrollView>

<ScrollView >
</ScrollView>

这是我的所有代码:http://pastebin.com/ysRhLMyt 当前屏幕: enter image description here 我想始终显示滚动条。

也许这个链接对你有用。https://dev59.com/8nM_5IYBdhLWcg3wZSE6 - kalpana
4个回答

3

请尝试,

  • Set the android:scrollbarFadeDuration="0"

               OR 
    
  • ScrollView1.setScrollbarFadingEnabled(false);

               OR
    
  • android:scrollbarFadeDuration="0" and

    android:scrollbarAlwaysDrawVerticalTrack="true" for vertical

    android:scrollbarAlwaysDrawHorizontalTrack="true" for horizontal

还有一件事,

请记住,ScrollView只能有一个子控件,因此我们可以将一个容器(线性、相对、表格布局)作为ScrollView的子项,并将所有控件放在这个子项中。

参考链接:http://android-pro.blogspot.com/2010/02/android-scrollview.html


这里使用了 android:scrollbarFadeDuration="0" 选项来显示滚动条。我无法拖动该条。我已在模拟器上尝试过。我将在设备上尝试并告知您。 - Piraba

1

尝试使用android:orientation属性。这可以用于水平或垂直:android:orientation="horizontal"android:orientation="vertical"

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:scrollbarFadeDuration="1000"
      android:scrollbarSize="12dip"
      android:background="@color/red" 
      android:orientation="horizontal">

      <TableLayout android:layout_width="match_parent"  
            android:layout_marginTop="10dp" 
            android:id="@+id/tableLayout1" 
            android:layout_height="wrap_content" 
            android:stretchColumns="1" 
            android:collapseColumns="2">
      </TableLayout>
</ScrollView>

1
以下是制作水平和垂直滚动视图的代码, 为了看到它的效果,首先定义一个大约 200x 200 dp 的区域,并将此代码粘贴到其中。
视图将会水平和垂直滚动。
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbarSize="10dp"
android:scrollbars="vertical" >

<HorizontalScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

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

            <Button
                android:layout_width="300dp"
                android:layout_height="100dp" />

            <Button
                android:layout_width="300dp"
                android:layout_height="100dp" />

            <Button
                android:layout_width="300dp"
                android:layout_height="100dp" />

            <Button
                android:layout_width="300dp"
                android:layout_height="100dp" />

            <Button
                android:layout_width="300dp"
                android:layout_height="100dp" />

            <Button
                android:layout_width="300dp"
                android:layout_height="100dp" />

            <Button
                android:layout_width="300dp"
                android:layout_height="100dp" />
        </LinearLayout>

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

            <Button
                android:layout_width="300dp"
                android:layout_height="100dp" />

            <Button
                android:layout_width="300dp"
                android:layout_height="100dp" />

            <Button
                android:layout_width="300dp"
                android:layout_height="100dp" />

            <Button
                android:layout_width="300dp"
                android:layout_height="100dp" />

            <Button
                android:layout_width="300dp"
                android:layout_height="100dp" />

            <Button
                android:layout_width="300dp"
                android:layout_height="100dp" />

            <Button
                android:layout_width="300dp"
                android:layout_height="100dp" />
        </LinearLayout>
    </LinearLayout>
</HorizontalScrollView>


1

嵌套滚动视图不起作用。这与滚动视图的触摸处理有关:顶层视图始终会消耗所有的触摸事件。您必须编写自己的自定义滚动视图。


我们如何定义自定义滚动视图? - Piraba
没有快速的解决方案。从ViewGroup子类化并实现所有内容。您可以获取Android ScrollView源代码并检查其实现方式。 - Dmitry Ryadnenko

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