当屏幕方向更改为横向时,如何为视图添加滚动条?

12

我有一个包含多个TextEdits的活动,在纵向模式下一切都显示完美。但当我将设备切换到横向模式时,一些视图没有被显示出来(它们被截断了)。我有什么办法可以在设备切换到横向模式时自动为视图添加滚动条呢?

2个回答

21

你可以尝试这样做,这可能对你有所帮助:

在你的布局中添加ScrollView并设置此标志android:fillViewport="true" 例如:

 <ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

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

        // your layout

    </LinearLayout>
</ScrollView>

当屏幕空间不足以容纳所有项目时,将启用滚动条。然后您可以滚动屏幕。 因此,如果屏幕方向更改为横向,则可以滚动项目,在纵向上则完美显示一切,无法滚动。


android:fillViewport="true" 这非常重要,对我很有用,谢谢 @Volodymyr - Adiii

0
检查您的设备是否处于竖屏模式并添加 :
ScrollView scroll = new ScrollView(yourContext);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
scroll.addView(yourView);

您应该查看文档:http://developer.android.com/reference/android/content/res/Configuration.html#orientation - Alexis C.
2
使用getResources().getConfiguration().orientation来获取当前屏幕方向。 - Sankar V

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