2011-01-28 51 views
8

"android:"という名前のテーマを上書きすることはできましたが、Android themes.xmlは上書きできないように見えるプロパティも定義しています。たとえば:デフォルトのAndroidテーマを上書きする

<!-- Variation on the Light theme that turns off the title --> 
<style name="Theme.Codebase" parent="android:style/Theme.Light"> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="colorBackground">@color/off_white</item> 
</style> 

colorBackgroundはTheme.LightのXMLで定義されていますが、ここではこれを追加すると私に

/res/values/styles.xml:10: error: Error: No resource found that matches the given name: attr 'colorBackground'. 

エラーを与えています。アプリケーションのスタイル全体をどのように上書きするのですか?

+0

アンドロイドのないこれらのタグは、アンドロイドソース内の同じres/valuesフォルダ内のattr xmlファイル内で定義されているようです。 http://android.git.kernel.org/?p=platform/frameworks/base.git;a=tree;f=core/res/res/values;hb=HEAD attrs.xmlとattrs_manifest.xmlを見てください何らかの理由でxmlnsを使ってこれらをインポートする必要があると思うか、類似のファイルを値フォルダに追加する必要があると思いますが、確かめるためにxmlについて十分に分かりません。 – Jems

答えて

8

あなたは標準を上書きすることができますが、あなたがwindowNoTitleなどの特性を変更したときと同じ方法を属性、ちょうどこのようandroid:接頭辞を追加することを忘れないでください:attrの接頭辞がなければ

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="SEclubTheme" parent="@android:style/Theme"> 
     <item name="android:colorForeground">@color/bright_foreground_dark</item> 
     <item name="android:colorBackground">@color/background_dark</item> 
    </style> 
</resources> 
+0

ええ、私はそれを考え出した。私はまだ "colorBackground"がテーマの中でどのように機能しているのか困惑しています。何か案が?今すぐ最高の答え! – typeoneerror

+0

私は言うことができます。 'colorBackgroundCacheHint'パラメータを定義します。このパラメータは、' ListView'スタイルでは、ビューが常に単色の背景に描画されるという仮定に基づいていくつかの最適化を実行するために使用されます。それに加えて、この属性のための使用がないように見えます。もっと重要なのは、名前が示すようにウィンドウの背景を定義する 'windowBackground'属性です。 – Malcolm

3

を、あなたのcolorBackgroundはあなたの属性になります定義する必要があります。この例では

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="?attr/theme_dependent_icon" /> 
</LinearLayout> 

、私が使用しているためカスタムテーマ名MyDarkThemeを:あなたは、あなたのmain_activity.xml?attr/theme_dependent_icon経由で属性を使用することができ、その後

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 
    <declare-styleable name="custom_menu"> 
      <attr name="theme_dependent_icon" format="reference"/> 
    </declare-styleable> 
    <style name="MyDarkTheme" parent="android:Theme" > 
     <item name="theme_dependent_icon">@drawable/ic_search_dark</item> 
    </style> 
    <style name="MyLightTheme" parent="android:Theme.Light" > 
     <item name="theme_dependent_icon">@drawable/ic_search_light</item> 
    </style> 
</resources> 

theme_dependent_iconstyles.xmlで定義されている次の例を考えてみましょうMyLightThemeの場合は、setContentViewより前のメインアクティビティのonCreateの間に選択する必要があります。つまり、

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setTheme(R.style.MyDarkTheme); // causes ic_search_dark.png to be shown 
    // setTheme(R.style.MyLightTheme); // causes ic_search_light.png to be shown 
    setContentView(R.layout.main_activity); 
} 

setTheme()を呼び出すことは、実行時にテーマを選択する1つの方法です。別の方法は、valuesvalues-11values-14の既定のテーマ、Android 3.0(API-11)のテーマ、Android 4.0(API-14)のテーマに対応するstyles.xmlの複数のバージョンを定義することです。

関連する問題