2014-01-30 50 views
0

如何從Java代碼中設置佈局和組件的樣式?類似以下內容是我想要做的:以編程方式設置Android樣式

button.setStyle(R.style.MyStyle); 

任何幫助表示讚賞。提前致謝。

回答

1

不能以編程方式應用樣式。雖然你可以像下面那樣做運行時充氣。

創建「template.xml」佈局文件。其中具有風格的TextView佈局。你可以創建更多的支持不同的風格。

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Template" 
    style="@style/first_style" /> 

在活動中,您可以通過以下代碼誇大此佈局。

TextView txtView = (TextView)getLayoutInflater().inflate(R.layout.template, null); 
+1

好的,我會那麼做。謝謝。 – ArmaAK

+1

@ArmaAK我認爲這是可能的搜索在stackoverflow http://stackoverflow.com/questions/11422220/android-how-to-programmatically-set-the-button-style-in-a-linearlayout – Raghunandan

+0

哇,我錯過了不知何故,當我最初看。這不會導致應用程序崩潰,但它也不會顯示。我得看看我能否得到這個工作。謝謝。 – ArmaAK

相關問題