ScrollView/TextView 设置 layout_gravity 时不显示所有数据

ScrollView/TextView not displaying all data when layout_gravity is set

每当 xml sheet 样式上设置 android:layout_gravity="bottom" 属性时,文本似乎在顶部被截断,并且在底部出现多余的空行。我设置这个是为了在有太多文本要显示时自动滚动到底部。这是托管在 DialogFragment.

注意:显示的文本来自 TextView.setText(Html.fromHtml()),如果重要的话,换行符作为 < br> 完成。打印出来的a只是测试数据。

在调试 TextView 时,似乎有数据(在 mText 变量中),但就是不显示。

对话框xmlsheet:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:fillViewport="true" >

        <TextView android:id="@+id/status_olcr_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" />

</ScrollView>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="?android:attr/buttonBarStyle" >

    <Button
        android:id="@+id/status_olcr_ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/ok"
        style="?android:attr/buttonBarButtonStyle"
        android:clickable="false" />

</LinearLayout>

正在发生的事情(但不应该发生)的屏幕显示:

这是应该发生的事情(当没有 layout_gravity 属性时):

知道发生了什么事吗?

更新:我已经打开 'Show layout margins' 并且 TextView 呈现在 ScrollView 边界上方(或其他方式)。见下图:

我建议您使用 RelativeLayout,您可以在其中将滚动视图设置为父级的顶部,将按钮线性布局设置为底部。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/llButton"
    android:alignParentTop="true"
    android:fillViewport="true" >

        <TextView android:id="@+id/status_olcr_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" />

</ScrollView>

<LinearLayout
    android:id="@+id/llButton"
    android:alignParentBottom="true"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="?android:attr/buttonBarStyle" >

    <Button
        android:id="@+id/status_olcr_ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/ok"
        style="?android:attr/buttonBarButtonStyle"
        android:clickable="false" />

</RelativeLayout>

最终采用了这个解决方案,虽然我仍然不明白一开始发生了什么......

在相关的 DialogFragment 中:

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

在 textView 标签内使用:

android:layout_gravity="top"