2012-03-01 7 views
1

ここに私の問題です。関連する.xmlファイルでさまざまなビューをカスタマイズするために、カスタムテーマとスタイルを定義しました。ここではいくつかのコードの抽出物は、以下のとおりです。Android - カスタムスタイルを微調整するための属性を使用

themes.xml:

... 
<style name="Legacy" parent="android:Theme.NoTitleBar"> 
    <item name="android:buttonStyle">@style/Legacy.Button</item> 
    ... 
</style> 

のstyles.xml:

... 
<style name="Legacy.Button" parent="@android:style/Widget.Button"> 
    <item name="android:textColor">#ffffff</item> 
    <item name="android:background">@drawable/button_selector_blue</item> 
    <item name="android:textSize">15dp</item> 
</style> 

のは、私はレガシーに自分のアプリケーションのテーマを設定しましょう。レイアウトでButtonを使用すると、カスタムパラメータ(白いテキスト、背景は@ drawable/button_selector_blueなど)が表示されます。

今度は、私はテキストサイズのために保存し、それらのパラメータを維持したいとしましょう:私はattrs.xmlでtitleSize属性で定義されるだろう、より大きな文字サイズ、といくつかのボタンを持っているしたいと思います:

... 
<attr name="titleSize" format="reference|dimension" /> 

と、themes.xmlファイルの各テーマにどの値が設定されていますか。

だから私のレイアウトは何か含まれます

<Button 
    android:id="@+idmyButtonId" 
    android:drawableRight="@drawable/aDrawable" 
    android:text="@string/someText" 
    android:textSize="?titleSize" 
    android:layout_width="fill_parent" android:layout_height="wrap_content"> 
</Button> 

私のアプリを起動すると、私は次のエラーを取得: java.lang.UnsupportedOperationExceptionが:ディメンションに変換できません:タイプ= 0x2の

をだから、私はカスタムスタイルを属性を使って調整することはできないようです - 少なくともこの方法ではありません。そんなことは可能ですか?そうでない場合は、そのような結果を得るために何を使用しますか?

私はユーザーに異なるテーマの中から選択する機能を与えたいので、追加のButtonWithLargeTextスタイルを定義してレイアウトで直接使用することはできません。

ありがとうございました!

+0

を! !! http://stackoverflow.com/questions/17103894/overriding-referenced-style-attributes – toobsco42

答えて

0

私はついにそれを働かせました。私のタイトルのサイズをattrs.xmlで定義する代わりに、私はdimens.xmlを使用しました。だから今、次の作品:

<Button 
    android:id="@+idmyButtonId" 
    android:drawableRight="@drawable/aDrawable" 
    android:text="@string/someText" 
    android:textSize="@dimen/titleSize" 
    android:layout_width="fill_parent" android:layout_height="wrap_content"> 
</Button> 

私はこれを使って、ボタンの上に(私は私のstyles.xmlで定義されている)私の通常のテキストサイズを取得中:私はここに似た何かをやっている

<Button 
    android:id="@+id/idRegularButton" 
    android:text="@string/regularSizeText" 
    android:layout_width="wrap_content" android:layout_height="wrap_content"> 
</Button> 
関連する問題