【问题标题】:Android add textview at position 0,0 is not workingAndroid 在位置 0,0 添加 textview 不起作用
【发布时间】:2015-05-26 16:23:52
【问题描述】:

我尝试将文本视图添加到位置 0,0 的相对布局!

使用以下代码:

TextView text = new TextView(getActivity());
    text.setTextColor(Color.RED);
    text.setIncludeFontPadding(false);
    text.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    text.setTextSize(TypedValue.COMPLEX_UNIT_PX, 32 * getResources().getDisplayMetrics().density);
    text.setText("TEST TEXT");
    text.setSingleLine(false);
    text.setX(0);
    text.setY(0);
    text.setTypeface(fct.getFont().getTypeFace(getActivity())); container.addView(text); 

容器是这里定义的相对布局:

<RelativeLayout
    android:id="@+frontcard/container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    />

我在 textview 中使用自定义字体...

问题是文本视图显示不正确。 顶部有一个填充...左侧设置正确。

输出图像:

Image as Textview

如果我把它画在画布上,一切正常,如下图所示:

Image as Bitmap

文本视图中的自定义字体有问题吗?

有人有什么想法吗?

【问题讨论】:

  • 我不确定,但 TextView 在顶部和底部有自然边距,试试 text.setGravity(Gravity.TOP);
  • 好主意...不幸的是它不起作用:/我猜自定义字体的填充有些问题

标签: android textview position


【解决方案1】:

尝试添加

text.setIncludeFontPadding(false);

【讨论】:

  • 或设置负上边距
  • 第 3 行我使用的正是这个...用户可以切换字体,所以静态负填充不是这样的解决方案
  • 是的,即使没有自定义字体,它也无法正常工作......顶部有一个填充,我不知道为什么
  • 这里是类似问题的答案:stackoverflow.com/a/6573535/4937232
  • 这不是一个好的解决方案......它只是为 textview 设置了一个负填充
猜你喜欢
  • 1970-01-01
  • 2015-01-14
  • 2016-04-24
  • 1970-01-01
  • 1970-01-01
  • 2014-07-19
  • 2021-10-22
  • 1970-01-01
  • 2016-09-22
相关资源
最近更新 更多