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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15897033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:13:31  来源:igfitidea点击:

How to add scrolling to the view when screen orientation is changed to landscape?

javaandroidlandscape

提问by Oleksandr Karaberov

I have an Activity with several TextEditsand in portrait mode everything is displayed perfectly.But when i switch the device to the landscape mode several views didn't displayed (they are cut).Can i in some way automatically add scrolling to the view when the device is switched to the landscape mode?

我有一个 Activity 有几个TextEdits在纵向模式下一切都完美显示。但是当我将设备切换到横向模式时,几个视图没有显示(它们被剪切)。我可以以某种方式自动添加滚动到视图时设备切换到横向模式?

回答by Volodymyr Yatsykiv

you can try do this, this maybe helpful to you:

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

add ScrollView to your layout and set this flag android:fillViewport="true"for example:

将 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>

Scroll will be enable when in your screen not enough space for items. Then you can scroll screen. So that if screen orientation is changed to landscape you can scroll items, and in portrait everything is displayed perfectly without ability to scroll.

当屏幕上没有足够的空间放置项目时,将启用滚动。然后你可以滚动屏幕。因此,如果屏幕方向更改为横向,您可以滚动项目,并且在纵向时,所有内容都可以完美显示而无法滚动。

回答by Alexis C.

Check when your device is in portrait mode and add :

检查您的设备何时处于纵向模式并添加:

ScrollView scroll = new ScrollView(yourContext);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
scroll.addView(yourView);