【问题标题】:Allign text inside TextView to it's border completely将 TextView 内的文本与其边框完全对齐
【发布时间】:2015-01-08 02:38:36
【问题描述】:

即使gravity 设置为top,文本与view 的边框之间也有一些填充。我需要发短信才能完全接触到边框。

您可以看到我正在努力消除的差距。

这是使用的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/someView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Here is some text"
        android:textSize="30dp"
        android:gravity="top" />
</LinearLayout>

【问题讨论】:

  • 什么是内置填充???我不知道那件事。我认为我们可以通过使用 TextView 的 android:gravity 属性来强制文本指向一个方向
  • 你到底想做什么?这是一个非常模糊的陈述,你是想让它左对齐还是右对齐?根据您的问题,我会说使用边距或填充,但显然这不是您需要的......
  • 感谢您的意见我编辑了这个问题,所以它更清楚(我希望)。 @zgc7009
  • android:includeFontPadding="false" 在您的 TextView 中。对不起,我一开始只是误解了你的问题,图片是为我做的:P
  • @zgc7009,已经尝试过了,不幸的是它仍然会留下空白

标签: android android-layout


【解决方案1】:

在xml中试试, android:paddingTop="-5dp"

负填充似乎总是对我有用。

手动设置高度而不是包裹内容。

线性布局是带有内边距的项目。您还需要修改该元素以删除空格。

【讨论】:

  • 好吧,在这种情况下不会
  • 针对不同的解决方案编辑
  • 这将从底部剪切文本,但不会将其移近边框。
【解决方案2】:

尝试创建 9.patch 行并将其设置为您的 TextView:android:drawableTop="@drawable/your_line"。 然后设置android:drawablePadding="-10dp"

这个技巧必须完美。

【讨论】:

  • 感谢您的解决方案是唯一有效的解决方案。不需要使用 9.patch,一个简单的color 也可以。我将编辑您的答案以显示布局。
【解决方案3】:

在你的 xml 文件中的 TextView 上试试这个:

android:includeFontPadding="false"

--

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/someView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Here is some text"
        android:textSize="30dp"
        android:includeFontPadding="false"
        android:gravity="top" />
</LinearLayout>

【讨论】:

  • 这一行有效。我不敢相信我是这个答案自创建以来的第一个赞成票:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-23
  • 2021-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-20
  • 2011-03-04
相关资源
最近更新 更多