天天看點

android 中resources管理

主要存在于res/value檔案夾中

定義:

dimen.xml:主要用于設定像素預設值

<resources>

 <code>res/values/dimens.xml</code>

&lt;dimen name="sp_12"&gt;12sp&lt;/dimen&gt;

&lt;dimen name="sp_13"&gt;13sp&lt;/dimen&gt;

 &lt;dimen name="dip_40"&gt;40dip&lt;/dimen&gt;

 &lt;dimen name="dip_45"&gt;45dip&lt;/dimen&gt;

&lt;/resources&gt;

代碼使用:

int width=getContext().getResources().getDimension(R.dimen.tab_width);

xml檔案使用:

android:layout_width="@dimen/tab_width"

color.xml設定顔色

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;item type="drawable" name="main_background"&gt;#FFE7E7E7&lt;/item&gt;

&lt;item type="drawable" name="newslist_item_background"&gt;#FFE7E7E7&lt;/item&gt;

&lt;item type="color" name="white"&gt;#FFFFFFFF&lt;/item&gt;

&lt;item type="color" name="category_title_normal_background"&gt;#FFADB2AD&lt;/item&gt;

&lt;item type="color" name="gold"&gt;#b89766&lt;/item&gt;

&lt;item type="color" name="transparent"&gt;#000000&lt;/item&gt;

&lt;item type="color" name="textcolor"&gt;#FFFFFF&lt;/item&gt;

&lt;item type="color" name="bgcolor"&gt;#000000&lt;/item&gt;

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;color name="contents_text"&gt;#ff000000&lt;/color&gt;

&lt;color name="encode_view"&gt;#ffffffff&lt;/color&gt;

&lt;color name="possible_result_points"&gt;#c0ffff00&lt;/color&gt;

&lt;color name="result_points"&gt;#c000ff00&lt;/color&gt;

&lt;color name="result_text"&gt;#ffffffff&lt;/color&gt;

&lt;color name="result_view"&gt;#b0000000&lt;/color&gt;

&lt;color name="share_text"&gt;#ff000000&lt;/color&gt;

&lt;color name="status_view"&gt;#50000000&lt;/color&gt;

&lt;color name="status_text"&gt;#ffffffff&lt;/color&gt;

&lt;color name="transparent"&gt;#00000000&lt;/color&gt;

&lt;color name="viewfinder_frame"&gt;#ff000000&lt;/color&gt;

&lt;color name="viewfinder_laser"&gt;#ffff0000&lt;/color&gt;

&lt;color name="viewfinder_mask"&gt;#60000000&lt;/color&gt;

&lt;color name="red"&gt;#ffff0000&lt;/color&gt;

&lt;color name="blue"&gt;#ff3590c4&lt;/color&gt;

&lt;color name="green"&gt;#7700ff00&lt;/color&gt;

&lt;color name="yellow"&gt;#ffff7000&lt;/color&gt;

&lt;color name="screen_background_white"&gt;#00ffffff&lt;/color&gt;

&lt;color name="translucent_background"&gt;#00000000&lt;/color&gt;

&lt;color name="white_background"&gt;#00ffffff&lt;/color&gt;

&lt;color name="gray_background"&gt;#fff7f7f7&lt;/color&gt;

&lt;color name="solid_red"&gt;#ffff0000&lt;/color&gt;

&lt;color name="solid_while"&gt;#ff888888&lt;/color&gt;

&lt;color name="solid_saffron"&gt;#ffff6414&lt;/color&gt;

&lt;color name="solid_blue"&gt;#ff0000ff&lt;/color&gt;

&lt;color name="solid_green"&gt;#ff00ff00&lt;/color&gt;

&lt;color name="solid_yellow"&gt;#ffffff00&lt;/color&gt;

&lt;color name="saffron"&gt;#ffffa649&lt;/color&gt;

&lt;color name="titlebackgroundcolor"&gt;#fffe8625&lt;/color&gt;

&lt;color name="listitem_divide"&gt;#ffd4d3d3&lt;/color&gt;

&lt;color name="gray"&gt;#ff666664&lt;/color&gt;

&lt;color name="deep_gray"&gt;#ff555555&lt;/color&gt;

&lt;color name="light_gray"&gt;#ff757575&lt;/color&gt;

&lt;color name="btn_focused_color_yellow"&gt;#fffdfdfd&lt;/color&gt;

&lt;color name="btn_pressed_color_light"&gt;#fffdfdfd&lt;/color&gt;

&lt;color name="btn_default_color_light"&gt;#ffff5f11&lt;/color&gt;

&lt;color name="rounded_container_border"&gt;#ffb7babb&lt;/color&gt;

&lt;color name="base_start_color_default"&gt;#ffffffff&lt;/color&gt;

&lt;color name="base_end_color_default"&gt;#ffffffff&lt;/color&gt;

&lt;color name="base_start_color_pressed"&gt;#ffff7a13&lt;/color&gt;

&lt;color name="base_center_color_pressed"&gt;#ffff8c2f&lt;/color&gt;

&lt;color name="base_end_color_pressed"&gt;#ffffa254&lt;/color&gt;

&lt;color name="text_color_default"&gt;#ff000000&lt;/color&gt;

&lt;color name="text_color_pressed"&gt;#ffffffff&lt;/color&gt;

調用color标簽:android:background="@color/red" @color/是調用res/下的比對的color标簽 

也可以将color标簽嵌入到selector的item标簽中使用  

&lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;

&lt;item android:state_pressed="true"

android:color="@color/red"/&gt;

&lt;item android:color="@color/green" /&gt;

&lt;/selector&gt;

代碼設定color方法:

testview.setTextColor(Color.parseColor("FFFFFF")); 

testview.setTextColor(Color.GRAY);

tv.setTextColor(Color.rgb(255, 255, 255)); 

testview.setTextColor(getContext().getResources().getColor(R.color.my_color));

array.xml

    &lt;declare-styleable name="DragSort"&gt;

        &lt;attr name="collapsed_height" format="dimension" /&gt;

        &lt;attr name="drag_scroll_start" format="float" /&gt;

        &lt;attr name="max_drag_scroll_speed" format="float" /&gt;

        &lt;attr name="float_background_color" format="color" /&gt;

        &lt;attr name="remove_mode"&gt;

            &lt;enum name="clickRemove" value="0" /&gt;

            &lt;enum name="flingRemove" value="1" /&gt;

        &lt;/attr&gt;

        &lt;attr name="track_drag_sort" format="boolean" /&gt;

        &lt;attr name="float_alpha" format="float" /&gt;

        &lt;attr name="slide_shuffle_speed" format="float" /&gt;

        &lt;attr name="remove_animation_duration" format="integer" /&gt;

        &lt;attr name="drop_animation_duration" format="integer" /&gt;

        &lt;attr name="drag_enabled" format="boolean" /&gt;

        &lt;attr name="sort_enabled" format="boolean" /&gt;

        &lt;attr name="remove_enabled" format="boolean" /&gt;

        &lt;attr name="drag_start_mode"&gt;

            &lt;enum name="onDown" value="0" /&gt;

            &lt;enum name="onMove" value="1" /&gt;

            &lt;enum name="onLongPress" value="2" /&gt;

        &lt;attr name="drag_handle_id" format="integer" /&gt;

        &lt;attr name="fling_handle_id" format="integer" /&gt;

        &lt;attr name="click_remove_id" format="integer" /&gt;

        &lt;attr name="use_default_controller" format="boolean" /&gt;

    &lt;/declare-styleable&gt;

 TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.DragSort, 0, 0);

         mItemHeight = Math.max(1, array.getDimensionPixelSize(R.styleable.DragSortListView_collapsed_height, 1));

         mTrackDragSort = array.getBoolean( R.styleable.DragSortListView_track_drag_sort, false);

xml使用:

    &lt;ListPreference

     android:title="@string/page_animation"

     android:defaultValue="@string/default_page_animation"

     android:entries="@array/page_animation_labels"

     android:entryValues="@array/page_animations"

     android:key="pageAnimation"

     /&gt;

strings.xml:主要設定文本

 &lt;string name="button_confirm"&gt;确定&lt;/string&gt;     

 &lt;string name="button_cancel"&gt;取消&lt;/string&gt;   

 &lt;string-array name="page_animations"&gt;

  &lt;item&gt;100&lt;/item&gt;

  &lt;item&gt;0&lt;/item&gt;

  &lt;item&gt;1&lt;/item&gt;

  &lt;item&gt;2&lt;/item&gt;

  &lt;item&gt;3&lt;/item&gt;

 &lt;/string-array&gt;

testview.setText(getString(R.string.button_confirm));

android:text="@string/button_confirm"

風格style與主題theme差別:

1:style主要用于View,theme主要用于Activity以及Application

2、style主要用于設定View的屬性,theme主要用設定Activity的樣式如是:否全屏

3、style與theme都是以&lt;style&gt;&lt;item&gt;文本&lt;/item&gt;&lt;/style&gt;建立,隻是item的内容不一樣

style定義:

style.xml檔案

 &lt;style name="TitleStyle"&gt; 

  &lt;item name="android:textSize"&gt;18sp&lt;/item&gt;

  &lt;item name="android:textColor"&gt;#ec9237&lt;/item&gt;

&lt;/style&gt;

&lt;style name="Title" parent="@style/TitleStyle"&gt; (繼承TitleStyle屬性)

  &lt;item name="android:textSize"&gt;5sp&lt;/item&gt; 

&lt;/style&gt; 

 &lt;/resources&gt;

&lt;EditText android:layout_height="wrap_content"

android:text="EditText"

style="@style/Title" 

android:layout_width="fill_parent" 

android:id="@+id/editText1" /&gt;

-----------------------------------------------

theme:

style.xml

1.&lt;?xml version="1.0" encoding="utf-8"?&gt;

  &lt;style name="theme1"&gt;

    &lt;item name="android:windowNoTitle"&gt;true&lt;/item&gt; 

    &lt;item name="android:windowFullscreen"&gt;?android:windowNoTitle&lt;/item&gt; 

  &lt;/style&gt; 

  &lt;style name="theme2.a"&gt;

&lt;/resources&gt; 

注意:setTheme應該在setContentView之前調用。

setTeme(R.theme.theme1);/setTeme(R.style.theme1)

setContentView(R.layout.main);

xml使用主題:

&lt;application

  android:icon="@drawable/icon" android:label="@string/app_name" 

  android:theme="@style/theme1"&gt;

  &lt;activity

    android:name=".MessageShowActivity" android:label="@string/app_name" 

    android:windowSoftInputMode="adjustPan" android:screenOrientation="portrait"

    android:theme="@style/theme2"&gt;

  &lt;/activity&gt;

&lt;/application&gt; 

---------------------------------

android的selector是在drawable/xxx.xml中建立的:

selector:主要用設定view點選選中顯示效果

  &lt;item android:state_window_focused="false" android:drawable="@drawable/pic1" /&gt;&lt;!-- 沒有焦點時的背景圖檔--&gt;

  &lt;item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/pic2" /&gt;&lt;!-- 非觸摸模式下獲得焦點并單擊時的背景圖檔--&gt;

  &lt;item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/pic3" /&gt;&lt;!-- 觸摸模式下單擊時的背景圖檔--&gt;

  &lt;item android:state_selected="true" android:drawable="@drawable/pic4" /&gt;&lt;!--選中時的圖檔背景--&gt;

  &lt;item android:state_focused="true" android:drawable="@drawable/pic5" /&gt;&lt;!--獲得焦點時的圖檔背景--&gt;

 &lt;/selector&gt;

在xml中使用示例:

<code>res/color/button_text.xml</code>

本文轉自歡醉部落格園部落格,原文連結http://www.cnblogs.com/zhangs1986/p/3624482.html如需轉載請自行聯系原作者

歡醉

繼續閱讀