【问题标题】:Set a style using java [duplicate]使用java设置样式[重复]
【发布时间】:2016-02-16 18:12:59
【问题描述】:

我创建了一个样式,添加到我的 textview xml 中并且工作正常,这样:

<TextView
                        android:id="@+id/userFeedName"
                        style="@style/feeds_user_connected_style"
                        android:text="@string/user_name"
                        />

但我需要使用java创建一个textview并使用我的风格,我试过了:

TextView textView = new TextView(this);

        textView.setTextAppearance(getApplicationContext(), R.style.feeds_user_connected_style);
        textView.setText(R.string.user_name);

但 android studio 抛出消息“setTextAppearance 已弃用”。我该如何解决它并使用我的风格?

【问题讨论】:

    标签: java android


    【解决方案1】:

    如果你的编译 sdk 版本是 23,你可以使用 setTextAppearance 方法没有上下文,如果是 R.style 文件,则只使用 id。

    也可以参考this question

    【讨论】:

      【解决方案2】:

      根据TextView docs,您应该在没有上下文的情况下使用setTextAppearance(int) sdk 级别 23 及以上。

      【讨论】:

      • 好的,我明白了。谢谢
      猜你喜欢
      • 2011-09-04
      • 2018-10-26
      • 2021-12-08
      • 2019-09-24
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 2021-06-17
      • 1970-01-01
      相关资源
      最近更新 更多