当我有一个TextView与一个\n在文本中,,在右边我有两个单线TextViews,一个在另一个之间没有间距。我已经为所有三个textview设置了以下内容。

android:lineSpacingMultiplier="1" 
android:lineSpacingExtra="0pt" 
android:paddingTop="0pt" 
android:paddingBottom="0pt"

第一行的左侧TextView线完美地与右上角TextView。

左TextView的第二行略高于右下TextView的第二行。

似乎有某种隐藏的填充顶部和底部的TextViews。我怎么才能去掉它呢?


当前回答

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/baselineImage"
        android:includeFontPadding="false" />

    <ImageView
        android:id="@+id/baselineImage"
        android:layout_width="1dp"
        android:layout_height="1dp"
        android:baselineAlignBottom="true"
        android:layout_alignParentBottom="true" />

    <!-- This view will be exactly 10dp below the baseline of textView -->
    <View
        android:id="@+id/view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/baselineImage" />

</RelativeLayout>

有了额外的ImageView,我们可以设置TextView基线对齐的ImageView和设置android:baselineAlignBottom上的ImageView为真,这将使ImageView基线底部。其他视图可以使用ImageView的底部来对齐自己,ImageView本身与TextView的基线相同。

然而,这只修复填充底部而不是顶部。

其他回答

这个技巧对我很管用(min-sdk >= 18)。

我使用android:includeFontPadding="false"和一个像android:layout_marginTop="-11dp"和把我的TextView在一个FrameLayout(或任何ViewGroup…)

最后是示例代码:

<LinearLayout
    android:layout_width="60dp"
    android:layout_height="wrap_content"
    >

    <TextView
        style="@style/MyTextViews.Bold"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/yellow"
        android:textSize="48sp"
        android:layout_marginTop="-11dp"
        android:includeFontPadding="false"
        tools:text="1"/>
</LinearLayout>

据我所知,这是大多数小部件固有的,不同的手机制造商“填充”的数量不同。这个填充实际上是图像边界和9补丁图像文件中的图像之间的空白。

例如,在我的Droid X上,旋转窗口小部件比按钮有更多的空白,这让你在旋转窗口内嵌一个按钮时看起来很尴尬,但在我妻子的手机上,同样的应用程序没有同样的问题,看起来很棒!

我唯一的建议是创建自己的9个补丁文件,并在应用程序中使用它们。

啊,Android的痛苦。

编辑:澄清填充和空白。

您可能想要尝试将左侧文本视图的底部与右侧第二个文本视图的底部对齐。

这也惹恼了我,我发现的答案是,实际上有额外的空间在字体本身,而不是TextView。对于一个文档出版背景的人来说,Android对排版元素的有限控制是相当令人恼火的。我建议使用自定义字体(如Bitstream Vera Sans,它是授权重新分发的),可能不会有这个问题。不过,我不确定它是否确实如此。

我认为这个问题可以这样解决:

 <TextView
        android:id="@+id/leftText"
        android:includeFontPadding="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="Hello World!\nhello world" />

 <TextView
        android:id="@+id/rightUpperText"
        android:includeFontPadding="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/leftText"
        android:layout_alignTop="@+id/leftText"
        android:textSize="30dp"
        android:text="Hello World!" />

 <TextView
        android:id="@+id/rightLowerText"
        android:includeFontPadding="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/leftText"
        android:layout_below="@+id/rightUpperText"
        android:textSize="30dp"
        android:text="hello world" />

结果如下:

ScreenShot-1

ScreenShot-3

虽然rightLowerText中的特殊字符行看起来比leftText中的第二行高一些,但它们的基线仍然是对齐的。