26

我计划在不同的设备大小上为 textview 使用不同的字体大小,以使字母清晰易读。我已经决定不为不同的设备使用不同的布局,并建立了一个通用的布局来适应所有的设备。现在唯一的问题是文本大小。

问题 :

  1. 我想就如何根据设备的大小(物理大小)更改字体大小获得您的技术建议。

  2. 如何根据纵横比得出字体大小。

  3. 使用这种方法有什么缺陷吗?

用于文本视图的 XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvValue4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="I planned to use different font size for the textview on different device size so as to make the letters legible. I have already decided not to use different layouts for different devices and built a common layout to fit in all the devices. Now the only problem is on the text size."
        android:textColor="#000000"
        android:textSize="15sp" />

</LinearLayout>

提前致谢

4

7 回答 7

27

是的,这种方法是有缺陷的。Android 设备有不同的尺寸,但它们也可以有非常不同的密度。

在此处输入图像描述

您应该只遵循 Android 设计最佳实践

在此处输入图像描述

他们实际上是经过深思熟虑的。为什么要重新发明轮子?

于 2013-03-30T05:33:55.853 回答
21

试试这个,在你的 xml 中添加这个属性。它将根据屏幕大小调整 textsize 试试看。

 style="@android:style/TextAppearance.DeviceDefault.Medium"
于 2013-03-30T05:36:15.787 回答
6

对于字体大小,使用比例像素 (sp)。Android 将根据设备密度相应地缩放字体大小。上面的帖子有更好的解释和推理。

于 2013-03-30T06:16:13.600 回答
2
    String s= "hello";
    TextView tv= (TextView) findViewById(R.id.tv);
    Spannable span = new SpannableString(s);
    span.setSpan(new RelativeSizeSpan(5f), 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    tv.setText(span);

根据屏幕尺寸将 5f 更改为您想要的任何内容。

http://developer.android.com/guide/practices/screens_support.html。检查标题最佳实践下的主题。

于 2013-03-30T07:31:31.970 回答
1

适用于 API 26 及更高版本

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:autoSizeTextType="uniform" />

来源:https ://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview

于 2019-11-11T13:10:56.867 回答
0

Android 已经为此内置了功能 - dp 和 sp。dp 是设备像素。它基本上是 1dp=1/160 英寸。这允许您以实际大小指定字体的高度。Sp 是缩放的像素。此大小基于默认字体大小进行缩放,因此用户可以放大他的系统字体并且您的应用程序会匹配它。对于需要大文本而不占用屏幕空间的有视力问题的人来说很方便。

您可能应该使用其中一种或另一种。

于 2013-03-30T05:33:47.527 回答
0

对于这个问题,我在这么多项目中使用以下库,并相信这太棒了。无需担心屏幕。但同样,您需要为选项卡创建单独的布局。

https://github.com/intuit/sdp

于 2018-01-11T10:15:54.390 回答