我正在使用TableLayout。我需要该布局的水平滚动和垂直滚动。默认情况下,我能够在视图中获得垂直滚动,但是水平滚动无法正常工作。我正在使用Android SDK 1.5 r3。我已经尝试过android:scrollbars="horizontal"。我在一些论坛上读到,在杯子蛋糕更新中,可以进行水平滚动。如何使我的布局在两个方向上滚动?
我找到了一种简单的方法来实现两种滚动行为。
这是它的xml代码:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:scrollbars="vertical">
<HorizontalScrollView
android:layout_width="320px" android:layout_height="fill_parent">
<TableLayout
android:id="@+id/linlay" android:layout_width="320px"
android:layout_height="fill_parent" android:stretchColumns="1"
android:background="#000000"/>
</HorizontalScrollView>
</ScrollView>
虽然有点晚了,但我希望这段代码能快速解决你的问题。 只需将你的代码放在下面的滚动视图中即可。
<HorizontalScrollView
android:id="@+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
//xml code
</ScrollView>
</HorizontalScrollView>
final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.leftMargin + lp.rightMargin, MeasureSpec.UNSPECIFIED);
由于其他解决方案都过时,要么效果不佳,要么根本不起作用,因此我修改了NestedScrollView
,它稳定、现代化,并且具备滚动视图所期望的所有功能。除了水平滚动。
这是仓库:https://github.com/ultimate-deej/TwoWayNestedScrollView
除了绝对必要的更改外,我没有对原始的NestedScrollView
进行任何“改进”。代码基于androidx.core:core:1.3.0
,这是撰写本文时的最新稳定版本。
以下所有内容均可正常工作:
NestedScrollView
)android:scrollbarAlwaysDrawHorizontalTrack="true"
例子:
<Gallery android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawHorizontalTrack="true" />
这个实现可以始终显示水平和垂直滚动条
activity_main.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- vertical scroll view -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:fadeScrollbars="false"
android:scrollbarSize="@dimen/scroll_bar_size">
<!-- horizontal scroll view hidden scroll bar -->
<HorizontalScrollView
android:id="@+id/real_horizontal_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<!-- content view -->
<EditText
android:id="@+id/real_inside_view"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</HorizontalScrollView>
</ScrollView>
<!-- fake horizontal scroll bar -->
<HorizontalScrollView
android:id="@+id/fake_horizontal_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:fadeScrollbars="false"
android:scrollbarSize="@dimen/scroll_bar_size">
<!-- fake content view that has width equals the real content view -->
<View
android:id="@+id/fake_inside_view"
android:layout_width="wrap_content"
android:layout_height="@dimen/scroll_bar_size" />
</HorizontalScrollView>
</RelativeLayout>
MainActivity.java
final EditText realInsideView = findViewById(R.id.real_inside_view);
final HorizontalScrollView realHorizontalSv = findViewById(R.id.real_horizontal_scroll_view);
final View fakeInsideView = findViewById(R.id.fake_inside_view);
final HorizontalScrollView fakeHorizontalSv = findViewById(R.id.fake_horizontal_scroll_view);
realHorizontalSv.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
fakeInsideView.setMinimumWidth(realInsideView.getWidth());
fakeHorizontalSv.setScrollX(scrollX);
}
});
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
</HorizontalScrollView>
ScrollView
与ListView
结合使用。这是因为ListView
内置了许多优化功能,这些功能与周围的ScrollView
不兼容。这里发布的答案没有使用ListView
,因此链接中的论点无关紧要。 - David Wasser