Android scrollView autosize

 ̄綄美尐妖づ 提交于 2019-12-11 04:17:12

问题


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:

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>

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.

Along with EditText I tried every possible iteration of using following elements and could not figure out a solution.

  • Scrollview
  • FrameLayout
  • LinearLayout

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.


回答1:


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>



回答2:


Use minLines and maxLines (of EditText) to specify the minimum and maximum the EditText can shrink/grow to, respectively.



来源:https://stackoverflow.com/questions/15505626/android-scrollview-autosize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!