setTextAppearance不推荐使用android如何使用?

时间:2016-07-05 09:37:34

标签: android

 layout=(RelativeLayout)findViewById(R.id.relate);
                TextView tv = new TextView(DetailActivity.this);
                layout.addView(tv);
                tv.setTextAppearance(DetailActivity.this, android.R.style.TextAppearance_Medium);
                tv.setGravity(Gravity.CENTER_HORIZONTAL);
                tv.setText("Hello Man");

以下是我的代码如何使用setTextAppearance for API> 23

帮助感谢

4 个答案:

答案 0 :(得分:18)

使用支持库中的TextViewCompat: TextViewCompat.setTextAppearance(tv,android.R.style.TextAppearance_Medium);

答案 1 :(得分:1)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                titleTextView.setTextAppearance(R.style.TextAppearance_MaterialComponents_Headline6);
            } else {
                titleTextView.setTextAppearance(context, R.style.TextAppearance_MaterialComponents_Headline6);
            }

答案 2 :(得分:0)

这意味着您不需要将上下文arg放在那里,您可以简单地设置样式ID。它现在自动获取上下文

答案 3 :(得分:0)

对于API> 23,做:

tv.setTextAppearance(android.R.style.TextAppearance_Medium);