Android:滚动视图问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2033296/
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-08-20 04:17:04  来源:igfitidea点击:

Android: ScrollView Issue

androidlayoutscrollview

提问by Niko Gamulin

I have had a number of problems with ScrollViews. Recently I tried to create a LinearView which content exceeds the screen size so I created the new layout with parent element ScrollView and set its width and height to custom values (the layout has to appear as dialog - not filling the whole screen).

我在使用 ScrollView 时遇到了许多问题。最近我试图创建一个内容超过屏幕尺寸的 LinearView,所以我创建了带有父元素 ScrollView 的新布局,并将其宽度和高度设置为自定义值(布局必须显示为对话框 - 而不是填充整个屏幕)。

When the element is selected the red border appears to embrace the rectangle of predefined ScrollView LayoutWidth and LayoutHeight which is OK. Then I placed inside the LinearView and set the values LayoutWidth and LayoutHeight to fill_parent.

当元素被选中时,红色边框似乎包含了预定义的 ScrollView LayoutWidth 和 LayoutHeight 的矩形,这是可以的。然后我放置在 LinearView 中并将值 LayoutWidth 和 LayoutHeight 设置为 fill_parent。

Then when I started to add the elements into the LinearLayout the border (red lile) wrapped the inserted elements, not the whole parent ScrollView.

然后当我开始将元素添加到 LinearLayout 时,边框(红色 lile)包裹了插入的元素,而不是整个父 ScrollView。

So when the content of the LinearLayout (child of ScrollView) exceeded the size of parent ScrollView the scroll didn't appear and the bottom elements are simply invisible.

因此,当 LinearLayout(ScrollView 的子级)的内容超过父级 ScrollView 的大小时,滚动不会出现并且底部元素根本不可见。

Anyway.. in that case the XML content was too extensive to post the question here and now I'm having the same problem with the more simple example.

无论如何..在这种情况下,XML 内容太广泛,无法在此处发布问题,现在我在使用更简单的示例时遇到了同样的问题。

The following part of the layout is intended to be a scrollable EditText (in case the user input exceeds the primary size of the widget). I wanted to set the EditText height to 120px, so I set the parent ScrollView height to 120px and the child EditText to fill_parent. Again in this case the EditText doesn't fill the whole ScrollView area but a single line:

布局的以下部分旨在成为可滚动的 EditText(以防用户输入超过小部件的主要大小)。我想将 EditText 高度设置为 120px,所以我将父 ScrollView 高度设置为 120px,将子 EditText 设置为 fill_parent。同样在这种情况下, EditText 不会填充整个 ScrollView 区域,而是一行:

 <ScrollView android:layout_height="120px" android:layout_width="fill_parent">
<EditText android:id="@+id/txtContent" android:layout_height="fill_parent" android:layout_width="fill_parent" android:text="TestContent">
</EditText>
</ScrollView>

Does anyone know how to reshape the content above so the EditText fills the parent control and in case the user's input exceeds the control size the scroll appears?

有谁知道如何重塑上面的内容,以便 EditText 填充父控件,如果用户的输入超过控件大小,则滚动出现?

Thank you!

谢谢!

Edit:

编辑:

Below are the screenshots of the layout

下面是布局截图

removed dead ImageShack link
removed dead ImageShack link

删除了无效的 ImageShack 链接
删除了无效的 ImageShack 链接

回答by Romain Guy

Your EditText should have a height of wrap_content. fill_parenthas no meaning in a ScrollView. If you want the EditTextto fill your ScrollViewwhen it's content is smaller than the ScrollView, use android:fillViewport="true"on the ScrollView.

您的 EditText 的高度应为wrap_content. fill_parent在 a 中没有意义ScrollView。如果您希望在内容小于 时EditText填充您ScrollView的内容ScrollView,请android:fillViewport="true"ScrollView.

回答by JonA

Yes, as Romain said, scrollview needs following combo.

是的,正如罗曼所说,滚动视图需要以下组合。

  1. for ScrollView android:fillViewport="true"
  2. for child of ScrollView android:layout_height="wrap_content"
  1. 滚动视图 android:fillViewport="true"
  2. 对于 ScrollView 的孩子 android:layout_height="wrap_content"