2013-07-02 19 views
2

我知道有很多關於setTextAppearance的討論,但我搜索,餘did't發現任何爲我工作...

我編程設置代碼爲一個textView,這是在一個表中。
但是,當我試圖從stylesView中調用styles.xml中的樣式時,它不適用。

從styles.xml:setTextAppearance()不工作textviews

<style name="ElemTabArtikl"> 
    <item name="android:layout_marginLeft">10dp</item> 
    <item name="android:textAppearance">?android:attr/textAppearanceMedium</item> 
</style> 

在Java代碼:

TextView tvNazArt = new TextView(this); 
    tvNazArt.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 0.4f)); 
    tvNazArt.setTextAppearance(this, R.style.ElemTabArtikl); 
    tvNazArt.setText(NazivArt); 
    tvNazArt.setPadding(10, 0, 0, 0); 
artikliVrstica.addView(tvNazArt); 

回答

3

如果你只使用你的風格兩個條目,爲什麼不只是做這一切編程?

變化:

tvNazArt.setTextAppearance(this, R.style.ElemTabArtikl); 

到:

float density = getResources().getDisplayMetrics().density; 
int padding = (int)(10 * density); 
tvNazArt.setPadding (padding, int top, int right, int bottom) 
tvNazArt.setTextAppearance(this, android.R.style.textAppearance_Medium); 

如果你想節省擊鍵,或必須這樣做了許多次,創建一個函數調用的setStyle(),或某事自然,並使用它來調用額外的代碼。

void setstyle(Context context) 
{ 
    float density = context.getResources().getDisplayMetrics().density; 
    int padding = (int)(10 * density); 
    tvNazArt.setPadding (padding, int top, int right, int bottom) 
    tvNazArt.setTextAppearance(context, android.R.style.textAppearance_Medium); 
} 
當然

,tvNazArt必須是全球性的。

+0

'android.R.style.textAppearanceMedium'應該是'android.R.style.TextAppearance_Medium'。 –

+0

謝謝,更新 – dberm22