2011-12-18 28 views
11

當我以編程方式調用setTextAppearance(this, android.R.style.TextAppearance_Medium)時,我得到的是中等大小的淺灰色文本字體。代碼與資源的setTextAppearance之間的區別

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

這些有什麼區別?

的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" /> 

的代碼看起來是這樣的:

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); 
} 

回答

1

它不應該是不同的。得到不同顏色的唯一方法是要麼:文字顏色與黑色:

1)您setTextAppearance

2)XML文件中包含的是Android編程後更改文本的顏色。

如果您的xml代碼包含android:textColor屬性,則Android將使用該屬性,而不是xml中定義的屬性。如果您手動設置文字外觀,則將使用該樣式的顏色。

因此,你可能有安卓:在XML中該元素文字顏色=「#000000」

+0

不,不改變文字顏色的任何地方 – user204884 2011-12-18 22:28:36

+0

還沒有一款Android :textColor =「」在xml中? – Entreco 2011-12-18 22:32:02

+0

nope - 查看更新的問題,包括代碼 – user204884 2011-12-18 23:01:30

8

正確的反應是setTextAppearance(context, android.R.style.TextAppearance_Large);

相關問題