2016-09-13 28 views
2

私はカスタム属性を使ってカスタムビューを実装しましたが、テーマにスタイルを設定しようとしています。私はthis answerの指示に従いましたが、私のウィジェットはアプリのテーマからスタイルを取り上げていません。アプリテーマにカスタムウィジェットのスタイルを追加する

カスタムは自分のウィジェットの属性:

<declare-styleable name="CustomTheme"> 
    <attr name="barGraphStyle" format="reference"/> 
</declare-styleable> 

スタイル私のウィジェット::

<style name="AppTheme.BarGraphStyle" parent="AppTheme"> 
    <item name="barColour">?attr/colorAccent</item> 
    <item name="barWidth">@dimen/bar_graph_bar_width</item> 
    <item name="maxBarHeight">@dimen/bar_graph_bar_max_height</item> 
    <item name="barWhiteSpace">@dimen/bar_white_space</item> 
</style> 

は私のアプリのテーマにスタイルを追加します。

<declare-styleable name="BarGraph"> 
    <attr name="barColour" format="color"/> 
    <attr name="barWidth" format="dimension"/> 
    <attr name="maxBarHeight" format="dimension"/> 
    <attr name="barWhiteSpace" format="dimension"/> 
</declare-styleable> 

は、スタイル参照を宣言します 私はstyle="@style/AppTheme.BarGraphStyle"を使用して、私のウィジェットに直接スタイルを適用した場合、

D/BarGraph(6862): BarGraph: barColour = null 
D/BarGraph(6862): BarGraph: barWidth = -1.0 
D/BarGraph(6862): BarGraph: maxHeight = -1.0 
D/BarGraph(6862): BarGraph: barWhiteSpace = -1.0 

:コンストラクタから

TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.BarGraph); 
ColorStateList barColour = styledAttributes.getColorStateList(R.styleable.BarGraph_barColour); 
Log.d(TAG, "BarGraph: barColour = " + barColour); 

float barWidth = styledAttributes.getDimension(R.styleable.BarGraph_barWidth, -1); 
float maxHeight = styledAttributes.getDimension(R.styleable.BarGraph_maxBarHeight, -1); 
float barWhiteSpace = styledAttributes 
      .getDimension(R.styleable.BarGraph_barWhiteSpace, -1); 
    styledAttributes.recycle(); 
Log.d(TAG, "BarGraph: barWidth = " + barWidth); 
Log.d(TAG, "BarGraph: maxHeight = " + maxHeight); 
Log.d(TAG, "BarGraph: barWhiteSpace = " + barWhiteSpace); 

ログ出力:

は最後に、私は、カスタム私のカスタムコンポーネントのコンストラクタ内の属性を取得しますそれは正しくスタイルするので、スタイル自体に問題はないとわかっています。

編集:私のコンストラクタ:

public BarGraph(Context context, @Nullable AttributeSet attrs) { 
    this(context, attrs, 0); 
} 

public BarGraph(Context context, @Nullable AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 

    // grab all the custom styling values 
    TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.BarGraph); 
    ColorStateList barColour = styledAttributes.getColorStateList(R.styleable.BarGraph_barColour); 
    Log.d(TAG, "BarGraph: barColour = " + barColour); 

    float barWidth = styledAttributes.getDimension(R.styleable.BarGraph_barWidth, -1); 
    float maxHeight = styledAttributes.getDimension(R.styleable.BarGraph_maxBarHeight, -1); 
    float barWhiteSpace = styledAttributes .getDimension(R.styleable.BarGraph_barWhiteSpace, -1); 
    styledAttributes.recycle(); 

    Log.d(TAG, "BarGraph: barWidth = " + barWidth); 
    Log.d(TAG, "BarGraph: maxHeight = " + maxHeight); 
    Log.d(TAG, "BarGraph: barWhiteSpace = " + barWhiteSpace); 

    // other non-styling code... 
} 
+0

カスタムコンポーネントのすべてのコンストラクタを投稿できますか? –

+0

@AndreClassenはそれらを追加しました – AesSedai101

+1

'AppTheme.BarGraphStyle'はテーマ全体を継承することは想定されていません。 '' ''や '' android:Widget "'は素晴らしい親です。それを見て、* theme *ではなく* style *であることをよりよく反映するように名前を変更します。 'Widget.BarGraph'はいいでしょう。 –

答えて

1

コンストラクタは次のようにする必要があります:

public BarGraph(Context context, @Nullable AttributeSet attrs) { 
     this(context, attrs, R.attr.barGraphStyle); 
    } 

    public BarGraph(Context context, @Nullable AttributeSet attrs, @AttrRes int defStyle) { 
     super(context, attrs, defStyle); 

     // grab all the custom styling values 
     TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.BarGraph, defStyle, 0); 

     ... 
    } 
+0

デフォルトを使用するだけではありませんか? – AesSedai101

+0

@ AesSedai101あなたは何を心配していますか? 'R.attr.barGraphStyle'は' AppTheme'で定義されたスタイルを参照します。問題がありますか? – nshmura

+0

ああ私はそれを逃した。 – AesSedai101

2

public BarGraph(Context context, AttributeSet attrs) { 
     this(context, attrs, R.attr.barGraphStyle); 
    } 

そして、あなたの第三のコンストラクタ内で、このような2番目のコンストラクタを変更、この行を使用

TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.BarGraph, defStyleAttr, R.style.AppTheme_BarGraphStyle); 

これらの行が見つかりませんでした。このコードで私にとって完璧に働いています。

+0

'BarGraph(Context、AttributeSet)'はXMLから膨らみながら呼び出されるコンストラクタです。 'R.attr.barGraphStyle'は、スタイルを指す(オプションの)テーマ属性です。 'R.style.AppTheme_BarGraphStyle'はデフォルト値を含むスタイルです。このデフォルトスタイルの値は、 'barGraphsStyle'テーマ属性や' style'属性や直接属性によって上書きされない限り、常に選択されます。 –

関連する問題