2016-11-30 5 views
0

私は別の場所で使用されるレイアウトを持っています。含まれているレイアウトのテキストの色を指定します。android

このレイアウト自体には、テキストビュー、いくつかのボタン、およびプログレスバーがあります。

<LinearLayout id="reuse"> 
    <TextView/> 
    <ProgressBar/> 
    <Buttons/> 
</LinearLayout> 

// in other places 

<include layout="reuse"/> // text color blue here 

// in other places 

<include layout="reuse" /> //text color gree here 

ここでレイアウトがどこに含まれているかによって、私はテキストビューに別のテキストの色を指定します。

どうすればいいですか?インクルードにtextcolorを指定しようとしましたが、これは役に立たないようですね。

+1

'include'が本当にインジケータ与えられた' layout'ということですので、私は、それが可能だとは思いませんその位置に「コピー」されます。パラメータに基づいてレイアウトを変更したい場合は、カスタムビューを作成し、カスタムビューを作成することをおすすめします。 –

答えて

1

私が考えることができる唯一の方法は、プログラム的に、リニアレイアウトのリファレンスを「再利用」し、reuse.findViewById()を使用してテキストビューを取得し、プロパティを自分で操作することです。

+0

では、textviewをtextColorを「継承」し、外側のレベルにテキストカラーを指定することは可能ですか? – user1017674

+0

いいえ、そのようなビューのプロパティは指定できません。 – avocado

0

xmlファイルでは作成できませんが、間接的にこれを達成できます。

まずオプション:

あなたは、プログラムでそれを行うことができます。たとえば:

private void setTextColors(ViewGroup viewGroup) { 
    for (int i = 0; i < viewGroup.getChildCount(); i++) { 
     View view = viewGroup.getChildAt(i); 
     if (view instanceof TextView) { 
      ((TextView) view).setTextColor(ContextCompat.getColor(this, android.R.color.holo_blue_bright)); 
     } else if (view instanceof Button) { 
      ((Button) view).setTextColor(ContextCompat.getColor(this, android.R.color.holo_blue_bright)); 
     }else if (view instanceof EditText) { 
      ((EditText) view).setTextColor(ContextCompat.getColor(this, android.R.color.holo_blue_bright)); 
     } else if (view instanceof LinearLayout) { 
      setTextColors((LinearLayout) view); 
     }else if (view instanceof RelativeLayout) { 
      setTextColors((RelativeLayout) view); 
     }else if (view instanceof FrameLayout) { 
      setTextColors((FrameLayout) view); 
     } 
    } 

} 

注:この方法は、単純な実施例です。必要に応じて変更することができます。

このメソッドを呼び出すと、(LinearLayoutのような)親レイアウトとしてパラメータが渡されます。ループの中で、親ビューのすべての子ビューをチェックし、それらがButton、TextView、またはEditTextのインスタンスである場合、必要な色を設定します。

<LinearLayout > 
    <TextView/> 
    <ProgressBar/> 
    <Button/> 
    <RelativeLayout> 
    <TextView/> 
    <ProgressBar/> 
    <Button/> 
    </LinearLayout> 
</LinearLayout> 

番目のオプション:あなたはtextViewsのためのあなたのstyles.xmlファイルにスタイルを作成し、テキストの色を与えることができ

また再帰的な、あなたの子ビュー・グループの色を設定しますこのスタイルに。 その後、レイアウトxmlのTextViewにスタイルを設定するだけです。例えば

<TextView 
    style="@style/CodeFont" 
    android:text="@string/hello" /> 

のstyles.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="CodeFont" parent="@android:style/TextAppearance.Medium"> 
     <item name="android:textColor">#00FF00</item> 
    </style> 
</resources> 
関連する問題