2012-09-25 31 views
0

我在將自定義方案(樣式)應用於某些視圖時遇到問題。我已經定義了自己的風格在res /價值/ styles.xmlAndroid自定義樣式/方案未正確應用於按鈕textColor

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 


    <style name="myStyle1"> 
     <item name="android:layout_width">fill_parent</item> 
     <item name="android:layout_height">wrap_content</item> 
     <item name="android:textColor">@color/red</item> 
     <item name="android:textColorHighlight">@color/blue</item> 
     <item name="android:textColorHint"> @color/fuchsia</item> 
     <item name="android:textColorLink">@color/olive</item> 
     <item name="android:typeface">serif</item> 
    </style> 

    <style name="AppTheme" parent="android:Theme.Light" /> 

</resources> 

我已經應用這種風格到我的簡單測試應用程序的主要活動,像這樣

<application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 


     <activity 
      android:name=".MainActivity" 
      android:label="@string/title_activity_main" 
      android:theme="@style/myStyle1" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
      . 
      . 
      . 
    </application> 

在MainActivity的佈局包含一些按鈕,一些textViews,其中一些具有明確的spiecified樣式屬性,其他人應該從整個活動中對其進行保護。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/layout_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/silver" 
    android:orientation="vertical" > 

    <EditText android:id="@+id/edit_message" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:hint="@string/edit_message" /> 

    <Button android:id="@+id/send_button" 
     style="@style/myStyle1" 
     android:text="@string/button_send" /> 

    <Button android:id="@+id/relative_activity" 
     android:text="@string/rel_activitity_text" 
     android:onClick="startRelativeActivity"/> 

    <Button android:id="@+id/button_TTT" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:textColor="@color/red" 
     android:text="@string/button_TTT" /> 

    <fragment android:name="my.firstapp.Fragment1" 
      android:background="@color/red" 
      android:id="@+id/list" 
      android:layout_weight="1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

</LinearLayout> 

據我所知,在活動中一切都應該按照我ahve定義的樣式風格化,但是,這是我得到

weird scheme

正如你所看到的,只有具有指定樣式的按鈕的文本呈紅色,而另外兩個則不會。但是,即使是白色的按鈕也能從方案中得到正確的字體,所以至少有一些正在被應用。爲什麼不是全部?任何幫助將不勝感激,我完全困惑。

回答

0

如果你想要一個全局風格的所有按鈕,你需要做這樣的事情。

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 

<style name="myStyle1"> 
    <item name="android:layout_width">fill_parent</item> 
    <item name="android:layout_height">wrap_content</item> 
    <item name="android:textColor">@color/red</item> 
    <item name="android:textColorHighlight">@color/blue</item> 
    <item name="android:textColorHint"> @color/fuchsia</item> 
    <item name="android:textColorLink">@color/olive</item> 
    <item name="android:typeface">serif</item> 
</style> 

<style name="AppTheme" parent="android:Theme.Light"> 
    <item name="android:buttonStyle">@style/myStyle1</item> 
</style> 

</resources> 

在你的Android清單應設置主題...

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 

    <activity 
     android:name=".MainActivity" 
     android:label="@string/title_activity_main" 
     android:theme="@style/AppTheme" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
     . 
     . 
     . 
</application> 

你可以做同樣的事情checkboxs等從這個鏈接的更多信息。 http://developer.android.com/reference/android/R.attr.html

此外,我測試了以下佈局。所有3個按鈕都應該有紅色文字。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/layout_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffaaaaaa" 
    android:orientation="vertical" > 

<EditText android:id="@+id/edit_message" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0" 
    android:hint="edit_message" /> 

<Button android:id="@+id/send_button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="button_send" /> 

<Button android:id="@+id/relative_activity" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="rel_activitity_text" 
    android:onClick="startRelativeActivity"/> 

<Button android:id="@+id/button_TTT" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0" 
    android:textColor="#ffff0000" 
    android:text="button_TTT" /> 

<!--<fragment android:name="my.firstapp.Fragment1" 
     android:background="#00ff0000" 
     android:id="@+id/list" 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" />--> 

</LinearLayout> 
+1

這不會以任何方式改變按鈕。更奇怪的是它不會改變任何東西。 textEdits仍然保持紫色,即使我期望這個AppTheme只定義了按鈕的樣式,其餘的都應該默認設置。 –

+0

您的MainActivity是否對按鈕做了任何圖形更改?這將覆蓋主題。 – frognosis

+0

Nope,setContentView(R.layout.activity_main);在onCreate()中幾乎是活動中唯一發生的事情 –

相關問題