Android scrollView自动调整大小 [英] Android scrollView autosize
问题描述
我是android开发的新手.我正在尝试做一些我认为简单的事情,但是经过大量的阅读,反复试验和确定之后,我仍然找不到一种简单而优雅的解决方案,因此我正在寻求有关以下方面的帮助:
I'm new to android development. I'm attempting to do something that I thought was simple but after much reading, trial and error and determination I've failed to find a solution I find simple and elegant and therefore I'm looking for help on how to approach the following:
我有两个UI元素,一个读取"TextView"的文本视图和一个EditText框:
I have two UI elemnents, a textview that reads "TextView" and an EditText box:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:ems="10"
android:gravity="top|left"
android:inputType="textMultiLine"
android:scrollbarAlwaysDrawVerticalTrack="false"
android:scrollbarStyle="insideOverlay" >
<requestFocus />
</EditText>
</LinearLayout>
我尝试实现的行为是: 当用户将文本添加到EditText框中时,它会扩展和增长-在某些时候,它的增长足以使TextView在屏幕外滚动.我希望TextView内容始终保持可见.因此,EditText或它所在的容器可能需要剪切其内容,而不是滚动整个屏幕.
The behavior I'm attempting to implement is: As a user adds text to the EditText box it expands and grows -- at some point it grows large enough to scroll the TextView off screen. I want the TextView content to remain visible at all times. Therefore the EditText or possibly a container it lives within needs to clip it's contents instead of scrolling the entire screen.
与EditText一起,我尝试了使用以下元素的所有可能的迭代,但找不到解决方案.
Along with EditText I tried every possible iteration of using following elements and could not figure out a solution.
- Scrollview
- FrameLayout
- LinearLayout
如果有可能,我宁愿在纯xml中实现此解决方案,以便可以保持良好的关注点分离.
If it's at all possible, I'd prefer to implement this solution in pure xml so that I can maintain good separation of concerns.
推荐答案
尝试将EditText
包装在ScrollView
内以使其可滚动.将ScrollView
layout_weight
属性设置为1
,将fillViewport
设置为true
将强制ScrollView填充可用空间,但绝不会将TextView
推出屏幕.
Try to wrap EditText
inside ScrollView
to make it scrollable. Setting ScrollView
layout_weight
attribute to 1
and fillViewport
to true
will force ScrollView to fill available space but never push TextView
out of the screen.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="top|left"
android:inputType="textMultiLine"
android:scrollbarAlwaysDrawVerticalTrack="false"
android:scrollbarStyle="insideOverlay" >
<requestFocus />
</EditText>
</ScrollView>
</LinearLayout>
这篇关于Android scrollView自动调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!