【问题标题】:How do I edit my fontsize programmatically?如何以编程方式编辑我的字体大小?
【发布时间】:2013-11-07 17:06:52
【问题描述】:

这个问题比题目更复杂。 我有一个 java 类,它映射来自服务器的值。现在我以编程方式设置文本大小,如下所示:

headerButton.setTextSize(Application.getAppResources().getDimensionPixelSize(R.dimen.manage_markets_text));

并将 manage_markets_text 的尺寸设置为 15sp 用于布局,对于 values-xlarge: 20sp 和对于 values-large 尺寸 18sp。现在,对于 xlarge 平板电脑,它可以达到 20sp,但是对于 htcone 手机,它映射到一些我什至不知道它来自哪里的大字体大小(似乎是 40-50sp)。现在我更改了代码,接受 getDimension 而不是 Pizelsize,但我仍然遇到同样的问题。如果我设置 settextsize(r.dimen.manage_market_text) ,它会显示空白页。我该如何解决这个问题?有什么线索吗?

【问题讨论】:

    标签: android android-layout android-fragments android-textattributes


    【解决方案1】:

    我想你会想在你的 XML 中设置它。

    <Button
        ...
        android:textSize="@dimen/manage_market_text"
        ...
    />
    

    另外,使用 values-xlarge 有点过时。根据

    http://developer.android.com/guide/practices/screens_support.html

    您应该将文件格式设置为类似于 values-sw600dp。

    要回答你原来的问题,只需写

    Resources res = this.getResources();
    headerButton.setTextSize(res.getDimension(R.dimen.manage_market_text))
    

    这应该可以解决问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-08
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多