【问题标题】:Make text bottom aligned even during overflow in Android即使在Android溢出期间也使文本底部对齐
【发布时间】:2011-10-10 15:59:55
【问题描述】:

我在RelativeLayout 中的文本视图中使用android:layout_alignParentLeft="true"android:layout_alignParentBottom="true"。它运作良好 - 将文本对齐在底部。但是,当文本大于容器时,它会溢出并开始在顶部对齐。我想让文本一直在底部对齐。如何做到这一点?很简单 - 我想实现正常的文本聊天行为而无需滚动。

没有溢出 - 没关系:

溢出 - 看不到底线,第 1 行总是在顶部:

我想要达到的目标:

【问题讨论】:

  • 如果您在 TextView 上调用 invalidate() 会怎样?
  • 不,那不是问题。即使在 android:text 中定义的长文本也会出现这种错误。

标签: android layout textview alignment


【解决方案1】:

您可以尝试通过 3 件事来解决此问题:

1) 你可以将android:singleLine="true" 放在 textview 的布局中,这样它就不会变得比它的容器大。

2) 尝试将android:gravity="bottom|left" 放入 textview 的布局中,看看是否会使 textview 与左下角对齐。

3) 做这样的布局:

    <?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">    
    <RelativeLayout 
        android:id="@+id/layout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >    
        <TextView 
            android:id="@+id/textView1" 
            android:layout_width="fill_parent"
            android:layout_alignParentLeft="true" 
            android:layout_height="wrap_content">
        </TextView>    
    </RelativeLayout>    
    </RelativeLayout>

更新:

另一个解决方案是让 textview 可滚动。只需设置android:maxLinesandroid:scrollbars = "vertical"。然后在你的java代码中添加TextView.setMovementMethod(new ScrollingMovementMethod()),你的textview就可以滚动了。

【讨论】:

  • 不行,还是不行。我使用了您的布局并获得了您在所附屏幕截图中看到的内容。
  • 我不确定这是否可行,但您可以尝试检查 textview 的大小是否大于屏幕的大小。如果是这样,则取 textview 大小和屏幕大小之间的差异,并将该值设置为 textview 的 marginTop。
  • 我不希望它是可滚动的! :)
  • 您是否尝试过我在评论中给出的解决方案,然后是:“检查我的更新”。
  • 负边距的东西是一种蛮力,但它有效。看来它必须保持这种状态。感谢您的想法!
猜你喜欢
  • 2020-07-09
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
  • 2013-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多