java - 基线对齐的TextView裁剪多行文本的底行

标签 java android textview

我希望android应用程序列表视图中的项目在左侧包含一个复选框,后面是一些文本,后面是一个按钮。我希望复选框和按钮与文本对齐。为了实现这一点,我将这三个项都放在一个linearlayout中,该linearlayout在默认情况下尝试基线对齐其项。这很好,除了多行文本的底线总是被剪裁。
下面是它的外观示例:
stackoverflow上有一个related question问题,建议的解决方案是为textview设置layout_gravity=“fill”,但由于它禁用了基线对齐,因此无法达到目的。这个问题有什么解决方法可以让我保持基线对齐吗?这似乎是许多人必须面对的一个基本问题。
我发现的另一个解决方案是在文本视图的底部添加一些填充,但这看起来很老套,而且不适合字体大小。我可以根据字体大小从代码中按程序设置填充,但是要获得正确的值可能会比较困难。
下面是一个说明问题的布局示例:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="8dp"
    android:paddingTop="8dp"
    android:baselineAligned="true">

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="9dp"
        android:layout_marginRight="8dp"
        android:textSize="16sp"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Woah, this is a long text line, enough that tit has to wrap to multiple lines, thus demonstrating our problem.  In fact, it wraps to multiple lines!  So many lines!  I hope they all show up correctly!"
            android:textSize="16sp"/>

    <FrameLayout
        android:layout_width="40dp"
        android:layout_height="match_parent">

        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="12dp"
                android:src="@drawable/ic_note_check_delete"/>
    </FrameLayout>
</LinearLayout>

最佳答案

我最后通过将TextView的布局重力更改为center_vertical解决了一个类似的问题。

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical" />

来自https://stackoverflow.com/a/11246243/3681880

关于java - 基线对齐的TextView裁剪多行文本的底行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16226354/

相关文章:

java - 用 Java 创建随机数生成器 - 每次都会重置数组

java - 对于 Java 中的按位运算,我是否需要关心 Java 中的大多数数字类型是否已签名?

java - 安卓 Realm : compactRealm not working as expected

LINUX 中的 Android SDK Manager 代理设置

java - 如何在 TextView 中使用 ClickableSpans 并且仍然能够水平滚动?

java - android TextView setText问题

java - 使用 ArrayList 和迭代器时出现 illegalStateException

java - Android NoClassDefFoundError - RSA 加密

Android Studio XML 布局文件存在但我找不到它

android - 小部件中的 EditText 替代品?必须有办法