2014-02-05 25 views
0

簡短問題: 是否可以爲單個視圖創建主題並設置特定樣式?個別視圖的具體樣式

可以說我希望TextView的文本顏色爲紅色,EditText的文本顏色爲綠色。

Somithing這樣的(但不工作):

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


    <item name="android:windowNoTitle">true</item> 
    <item name="android:textColor">#0000ff</item> 

    <style parent="@android:style/Widget.EditText"> 
     <item name="android:textColor">#ff0000</item> 

    </style> 
</style> 

UPDATE:

我的應用程序的核心元素是意見我對我自己的畫。爲了支持主題,我有一個抽象的「主題」基類,它有方法來提供我需要的所有顏色和drawable。從這堂課我得到幾個類像「BlackTheme」,「WhiteTheme」... 用戶可以在設置中設置正確的主題。對於用戶需要的每個附加信息,我都會使用帶默認Android小部件的DialogFragment。在DialogFragments中,我在onCreateView中應用對話框樣式。

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    ReleaseNotesPopup viewLayout = new ReleaseNotesPopup(getActivity(), _callback, this); 

    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getDialog().getWindow().setSoftInputMode(STYLE_NO_INPUT); 


    viewLayout.setBackgroundDrawable(this.getResources().getDrawable(_Theme.popupBackground())); 

    return viewLayout; 
} 

我的核心問題是,當我有一個黑色背景時,一些小部件變得不可見。因此,我需要自定義選擇器。不幸的是,您無法以編程方式應用小部件樣式。這就是爲什麼我這樣做的方法是約瑟夫在他的回答中描述: attrs.xml

<resources> 

<attr name="myEditTextStyle" format="reference" /> 
<attr name="myCheckBoxStyle" format="reference" /> 

styles.xml

<style name="WhiteTheme" parent="AppBaseTheme"> 
    <item name="android:windowNoTitle">true</item> 
</style> 


<style name="AppThemeBlack" parent="AppBaseTheme"> 


    <item name="android:windowNoTitle">true</item> 
    <item name="android:textColor">#b1b1b1</item> 
    <item name="myEditTextStyle">@style/EditTextmyTime</item> 
    <item name="myCheckBoxStyle">@style/CheckBoxmyTime</item> 
</style> 

我已經加入:風格=「ATTR/myCheckBoxStyle 「我所有的複選框。 如果BlackTheme是主動的,我做setTheme(R.style.BlackTheme);在我的Activity.onCreate()

對於BlackTheme我需要特殊的選擇器,因爲當背景爲黑色時,默認的checked/unchecked圖標是不可見的。

對於具有較高對比度的主題,可以說WhiteTheme,我不設置項目「myCheckBoxStyle」。

這implementaion工作,但我想這是不是最佳的...

UPDATE2:

這是我的複選框類型。從下載:http://android-holo-colors.com/

<?xml version="1.0" encoding="utf-8"?> 


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

<style name="EditTextmyTime" parent="android:Widget.EditText"> 
    <item name="android:background">@drawable/mytime_edit_text_holo_dark</item> 
    <item name="android:textColor">#ffffff</item> 
</style> 

<style name="CheckBoxmyTime" parent="android:Widget.CompoundButton.CheckBox"> 
    <item name="android:button">@drawable/mytime_btn_check_holo_dark</item> 
</style> 

</resources> 

<?xml version="1.0" encoding="utf-8"?> 


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

<!-- Enabled states --> 



<item android:drawable="@drawable/mytime_btn_check_on_holo_dark" android:state_checked="true" android:state_enabled="true" android:state_window_focused="false"/> 
<item android:drawable="@drawable/mytime_btn_check_off_holo_dark" android:state_checked="false" android:state_enabled="true" android:state_window_focused="false"/> 
<item android:drawable="@drawable/mytime_btn_check_on_pressed_holo_dark" android:state_checked="true" android:state_enabled="true" android:state_pressed="true"/> 
<item android:drawable="@drawable/mytime_btn_check_off_pressed_holo_dark" android:state_checked="false" android:state_enabled="true" android:state_pressed="true"/> 
<item android:drawable="@drawable/mytime_btn_check_on_focused_holo_dark" android:state_checked="true" android:state_enabled="true" android:state_focused="true"/> 
<item android:drawable="@drawable/mytime_btn_check_off_focused_holo_dark" android:state_checked="false" android:state_enabled="true" android:state_focused="true"/> 
<item android:drawable="@drawable/mytime_btn_check_off_holo_dark" android:state_checked="false" android:state_enabled="true"/> 
<item android:drawable="@drawable/mytime_btn_check_on_holo_dark" android:state_checked="true" android:state_enabled="true"/> 

<!-- Disabled states --> 

<item android:drawable="@drawable/mytime_btn_check_on_disabled_holo_dark" android:state_checked="true" android:state_window_focused="false"/> 
<item android:drawable="@drawable/mytime_btn_check_off_disabled_holo_dark" android:state_checked="false" android:state_window_focused="false"/> 
<item android:drawable="@drawable/mytime_btn_check_on_disabled_focused_holo_dark" android:state_checked="true" android:state_focused="true"/> 
<item android:drawable="@drawable/mytime_btn_check_off_disabled_focused_holo_dark" android:state_checked="false" android:state_focused="true"/> 
<item android:drawable="@drawable/mytime_btn_check_off_disabled_holo_dark" android:state_checked="false"/> 
<item android:drawable="@drawable/mytime_btn_check_on_disabled_holo_dark" android:state_checked="true"/> 

</selector> 

乾杯, 斯特凡

回答

0

如果要對單個元素的完全控制(即第2個textviews可能會有所不同,或者在不同的textviews頁面可能會有所不同),但希望它可以通過主題進行控制:

RES /值/ styles.xml

<resources> 
    <style name="MyAppTheme" parent="android:Theme.Light"> 
     <item name="android:windowNoTitle">true</item> 
     <item name="myTextAppearance">@style/MyTextAppearance</item> 
     <item name="myEditTextStyle">@style/MyEditTextStyle</item> 
    </style> 

    <style name="MyTextAppearance" parent="android:TextAppearance"> 
     <item name="android:textColor">#0000ff</item> 
    </style> 

    <style name="MyEditTextStyle" parent="android:Widget.EditText"> 
     <item name="android:textColor">#ff0000</item> 
    </style> 
</resources> 

RES /值/ ATTRS。XML

<resources> 
    <attr name="myTextAppearance" format="reference" /> 
    <attr name="myEditTextStyle" format="reference" /> 
</resources> 

RES /佈局/ layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Hello World" 
     android:textAppearance="?attr/myTextAppearance" 
     /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Message Here" 
     style="?attr/myEditTextStyle" 
     /> 

</LinearLayout> 

如果你不想在單個視圖控件,你可以覆蓋默認值:

RES/values/styles.xml

<resources> 
    <style name="MyAppTheme" parent="android:Theme.Light"> 
     <item name="android:windowNoTitle">true</item> 
     <item name="android:textColorPrimary">#0000ff</item> 
     <item name="android:editTextStyle">@style/MyEditTextStyle</item> 
    </style> 

    <style name="MyEditTextStyle" parent="android:Widget.EditText"> 
     <item name="android:textColor">#ff0000</item> 
    </style> 
</resources> 

RES /佈局/ layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Hello World" 
     /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Message Here" 
     /> 

</LinearLayout> 
+0

其實我想避免這種因爲我不想樣式添加到每個個人看法。我想在主題中設置一次。 – stefan

+0

此外,用戶應該有可能在設置中更改主題。我已經爲我自己繪製的視圖定製了主題。我通過實現一個抽象的主題類和幾個實現返回主題特定的顏色和drawables ... 上面發佈的方法不會滿足我的需要,因爲這將需要每個主題的佈局文件。其實我只是想這麼叫「Activity.setTheme(R.drawable.RedTheme)」根據用戶設置 – stefan

+0

更新了我的答案並使其成爲主題 –