如何在线性布局中显示最后一个文本视图

How to display last textview in linear layout

想在 ScrollView 中的 LinearLayout 中显示最后一个文本视图吗?

这是我的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.qianonnphoon.tradeal.chat.ChatActivity"
android:background="#ffffff"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical">

<ScrollView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/scrollView">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/layout1">

</LinearLayout>
</ScrollView>

<include
layout="@layout/message_area"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_marginTop="5dp"/>
</LinearLayout>

TextView 将在收到数据时添加到 layout1。所以一旦完成加载,我将在 layout1 中有多个文本视图。但是,它显示第一个 TextView,然后我需要向下滚动才能看到最后一个 TextView。完成加载时显示最后一个 TextView 有什么想法吗?

这是我在接收数据时添加 TextView 的代码

TextView textView = new TextView(ChatActivity.this);
    textView.setText(message);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lp.setMargins(0, 0, 0, 10);
    textView.setLayoutParams(lp);

    if(type == 1) {
        textView.setBackgroundResource(R.drawable.rounded_corner1);
    }
    else{
        textView.setBackgroundResource(R.drawable.rounded_corner2);
    }

    layout.addView(textView);
    scrollView.fullScroll(View.FOCUS_DOWN);

这个解决方案对我有用

final ScrollView scrollview = ((ScrollView) findViewById(R.id.scrollview));
scrollview.post(new Runnable() {
    @Override
    public void run() {
        scrollview.fullScroll(ScrollView.FOCUS_DOWN);
    }
});