如何以编程方式为TextView提供5dp换行符

问题描述 投票:8回答:2

我想以编程方式更改TextView的行距。我搜索发现setLineSpacing。问题是,它有两个参数,我尝试了很多值,但无法获得想要的结果。我只需要给TextView 5dp换行符,应该在方法中添加5 dp换行符?

android textview
2个回答
21
投票

您为什么不能使用setLineSpacing?那正是我要用的。

基于Android Documentation

public void setLineSpacing(浮动添加,浮动多位)

每行的高度乘以多乘,并加成。

因此,这是您可以选择执行的操作:

myTextView.setLineSpacing(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5.0f,  getResources().getDisplayMetrics()), 1.0f);

或者您可以修改android:lineSpacingExtra的XML布局。 (请参见Android Documentation。)

<TextView
    android:id="@+id/txtview"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:lineSpacingExtra="5dp" />

0
投票

如果提取维资源:

setLineSpacing(resources.getDimension(R.dimen.dp5), 1.0f)
© www.soinside.com 2019 - 2024. All rights reserved.