需要使TextView自动垂直滚动



我正在尝试开发一个活动,该活动将成为大学项目的简单聊天应用程序的一部分。我想设置一个EditTextButton,当按下时,它们会获取EditText的文本,并将其附加到同一活动中TextView的内容中。到目前为止,这对我来说似乎很好,但我也想让它在发送消息时显示在TextView上,如果没有足够的空间显示新的文本行,它将自动向下滚动。

这就是我在活动中的onCreate()

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText edit = (EditText) findViewById(R.id.editText1) ;
    final Button button = (Button) findViewById(R.id.button1) ;
    final TextView text = (TextView) findViewById(R.id.textView1) ;
    //text.setMaxHeight(200);
    text.setSelected(true);
    text.setMovementMethod(new ScrollingMovementMethod());
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(edit.getText().length() > 0) {
                text.append(edit.getText() + "n") ;
                edit.setText("");
            }
        }
    });
}

这是"活动"的XML布局文件的相关部分。

<TextView
    android:id="@+id/textView1"
    android:ellipsize="marquee"
    android:focusable="false"
    android:marqueeRepeatLimit="marquee_forever"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="70dp"
    android:text=""
    android:scrollbars ="vertical"
    android:maxLines="10" />

我做错了什么?还是我误解了,我想要的东西在文本视图中是不可能的?

EDIT:完整的XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/editText1"
    android:layout_alignBottom="@+id/editText1"
    android:layout_alignParentRight="true"
    android:text="@string/okString" />
<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="107dp"
    android:layout_toLeftOf="@+id/button1"
    android:ems="10" />
<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" >
    <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="70dp"
            android:ellipsize="marquee"
            android:focusable="false"
            android:marqueeRepeatLimit="marquee_forever"
            android:maxLines="10"
            android:scrollbars="vertical"
            android:text="     " />
</ScrollView>

            String text="Im trying to develop an activity that will be part of a simple chat application for a college project. I want to have a setup where I have an EditText and a Button which when pressed takes the  "
            mText = (TextView) findViewById(R.id.textView1);
            mText.setText(text)
    mText.setMovementMethod(new ScrollingMovementMethod())

XML

    <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/black"
    android:text="                       
                    " />

注意:Instead of storing your text as normal string save it in HTML format which will add line break in your text.

 String text="Im trying to develop an activity that will<br> be part of a simple chat <br>application for a college project. I want to have a setup where I have an EditText and <br>a Button which when pressed takes the  "

然后将这一行添加到上面的代码中

 mText.setText(Html.fromHtml(text));

这将返回更正确的结果

你不应该把垂直滚动的组件放在垂直滚动的滚动视图中,因为你的文本视图是可滚动的,我不明白你为什么首先需要滚动视图。然后你可以使用text.setScrollY(text.getLayout().getHeight())。我不确定它是否有效,但如果无效,你可以尝试在TextView 中获取文本的大小

假设您已经在xml文件中插入了一个结束标记</RelativeLayout>,我尝试了您的代码,它在我端运行良好。请尝试清理项目并从重新构建

Project -> Clean
Project -> Build Project

编辑:你能试试这个版式吗?请注意,我已经从布局中删除了ScrollView,并再次插入了text.setMovementMethod(new ScrollingMovementMethod());行。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent">
<TextView 
    android:text="    " 
    android:id="@+id/textView1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:scrollbars="vertical"
    android:maxLines="10">
</TextView>
<RelativeLayout 
    android:id="@+id/InnerRelativeLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >
    <Button 
        android:text="Button1" 
        android:id="@+id/button1"
        android:layout_alignParentRight="true" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </Button>
    <EditText 
        android:id="@+id/editText1" 
        android:layout_width="fill_parent"
        android:layout_toLeftOf="@id/button1"
        android:layout_height="wrap_content">
    </EditText>
</RelativeLayout>

最新更新