从手机到平板电脑时如何增加字体大小

How to increase font size when going from mobile to tablet

我想知道我是否可以为手机和平板电脑制作一个通用的应用程序。现在我已经开发了一个带有按钮的移动应用程序。但是当我去平板电脑时,按钮大小保持不变。我想在转到平板电脑时增加按钮大小。

实际上,我们鼓励您在平板电脑中更好地利用更多 space,例如Fragments 为不同的屏幕尺寸使用不同的布局。

但是,如果您只是想让平板电脑的东西变大,您可以简单地使用 resource qualifier in android。在您的情况下,在 res 文件夹中创建另一个值目录,例如values-sw600dp for tablet with smallest width from 600dp, and then put dimens.xml inside the folder.

在那里面 res/values-sw600dp/dimens.xml

<resources>
    <!-- Customization of dimensions in res/values/dimens.xml -->
    <dimen name="button_layout_width">200dp</dimen>
    <dimen name="button_layout_height">100dp</dimen>
    <dimen name="button_text_size">20sp</dimen>   
</resources>

另外,在res/values/dimens.xml

中将相同的项目放入默认的dimens.xml
<resources>
    <!-- Default dimension for phone  -->
    <dimen name="button_layout_width">120dp</dimen>
    <dimen name="button_layout_height">48dp</dimen>
    <dimen name="button_text_size">12sp</dimen>        
</resources>

然后在您的布局中使用它们 xml 文件

<Button
    android:layout_width="@dimen/button_layout_width"
    android:layout_height="@dimen/button_layout_height"
    android:textSize="@dimen/button_text_size"
    android:text="My Button"
    />

系统会根据限定符为您使用正确的值。在您的情况下,请根据您的应用 运行 所在设备的最小宽度选择正确的尺寸。

了解有关 resource qualifier here

的更多信息