如何将可绘制矢量调整为设备字体大小

时间:2019-01-31 21:51:02

标签: android android-drawable android-vectordrawable

是否可以设置可绘制的矢量,以根据设备设置的字体大小自动调整自身大小?每当我在“设置”菜单中更改字体大小时,矢量可绘制对象似乎都不会适应它的大小,并且始终保持不变,但是文本不会出现错误。

可绘制

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="48dp" android:width="48dp" android:viewportHeight="24" android:viewportWidth="24">
    <path android:fillColor="#FFFFFF" android:pathData="M7.41,7.84L12,12.42l4.59,-4.58L18,9.25l-6,6 -6,-6z"/>
</vector>

小字体

enter image description here

常规字体大小

enter image description here

大字体大小

enter image description here

巨大的字体大小

enter image description here

2 个答案:

答案 0 :(得分:1)

可以在可绘制矢量的定义中使用sp单位。 sp单位的要点是,当字体大小正常时,1sp == 1dp,但是当字体大小正常时,1sp大于1dp放大。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="48sp" <!-- use sp here instead of dp -->
    android:width="48sp"  <!-- use sp here instead of dp -->
    android:viewportHeight="24"
    android:viewportWidth="24">

    <path
        android:fillColor="#FFFFFF"
        android:pathData="M7.41,7.84L12,12.42l4.59,-4.58L18,9.25l-6,6 -6,-6z"/>

</vector>

答案 1 :(得分:1)

  • 作为先前的答案,请使用sp代替dp,
  • sp更适合文字
  • dp更适合图片