代码与资源之间的setTextAppearance之间的区别

use*_*884 11 android

当我以setTextAppearance(this, android.R.style.TextAppearance_Medium)编程方式调用时,我得到中等大小的字体和浅灰色文本.

但是,当我android:textAppearance="@android:style/TextAppearance.Medium"在xml中使用时,我得到相同大小的文本,但它是黑色的.

这些有什么区别?

xml文件的相关部分如下所示:

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="@android:style/TextAppearance.Medium"
    android:text="Button" />
Run Code Online (Sandbox Code Playgroud)

代码如下所示:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button button = (Button)findViewById(R.id.button1);
    button.setText("This is a long text");
    //button.setTextAppearance(this, android.R.style.TextAppearance_Medium);
}
Run Code Online (Sandbox Code Playgroud)

use*_*390 8

正确的反应是 setTextAppearance(context, android.R.style.TextAppearance_Large);


Ent*_*eco 1

应该没有什么不同。获得不同颜色的唯一方法是:

1)在 setTextAppearance 之后以编程方式更改文本颜色

或者

2)您的xml文件包含一个带有黑色的android:textColor。

如果您的 xml 代码包含 android:textColor 属性,Android 将使用该属性,而不是 xml 中定义的属性。如果您手动设置TextAppearance,则会使用样式的颜色。

因此,您可能在该元素的 xml 中有 android:textColor="#000000"