【问题标题】:TableLayout ScrollView Vertical & HorizontalTableLayout ScrollView 垂直和水平
【发布时间】:2011-11-08 04:03:42
【问题描述】:

我们如何垂直和水平设置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

当前屏幕:

我想一直显示滚动条。

【问题讨论】:

标签: android scrollbar


【解决方案1】:

试试,

  • 设置android:scrollbarFadeDuration="0"

               OR 
    
  • ScrollView1.setScrollbarFadingEnabled(false);

               OR
    
  • android:scrollbarFadeDuration="0"

    android:scrollbarAlwaysDrawVerticalTrack="true" 表示垂直

    android:scrollbarAlwaysDrawHorizo​​ntalTrack="true" 表示水平

还有一件事,

请记住,ScrollView 只能有一个子控件,因此我们可以将容器(Linear、relative、Table Layouts)作为 ScrollView 的子控件,并将所有控件放在该子控件中。

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

【讨论】:

  • 这是显示带有android:scrollbarFadeDuration="0" 选项的滚动条。我无法拖动滚动条。我在模拟器上尝试过。我会在设备上尝试并通知您
【解决方案2】:

嵌套滚动视图不起作用。它与滚动视图触摸处理有关:顶级视图总是消耗所有触摸事件。您必须编写自己的自定义滚动视图。

【讨论】:

  • 我们如何定义自定义滚动视图?
  • 没有快速的食谱。 ViewGroup 的子类并实现所有的东西。您可以获取 android ScrollView 源并检查它是如何实现的。
【解决方案3】:

下面的代码生成水平和垂直滚动视图, 要查看其效果,请先在 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>

【讨论】:

    【解决方案4】:

    尝试使用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>
    

    【讨论】: