Android之旅十六 android中各種資源的使用,android之旅資源

來源:互聯網
上載者:User

Android之旅十六 android中各種資源的使用,android之旅資源

android中各種資源的使用:

    在android開發中,各種資源的合理使用應該在各自的xml中進行定義,以便重複使用;

    字串資源:strings.xml,xml中引用:@string/XXX,java代碼中引用:R.string.XXX

     樣式資源:styles.xml,xml中引用:@style/XXX,java代碼中引用:R.style.XXX

     圖片資源:colors.xml,xml中引用:@color/XXX,java代碼中引用:R.color.XXX

     尺寸資源:dimens.xml,xml中引用:@dimen/XXX,java代碼中引用:R.dimen.XXX

     .............

根據各資源定義,實現的簡單的 :

修改頁面配置背景:android:background="#f0f0e0",也可以引用color.xml中定義的資源

1、在values檔案夾中建立colors.xml檔案:

<?xml version="1.0" encoding="utf-8"?><resources><color name="tv_text_color">#FF33CC</color><color name="btn_text_color">#0033CC</color></resources>

上面中定義了TextView和Button中字型顏色,頁面使用:@color/tv_text_color

   <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="測試字型樣式"        android:textColor="@color/tv_text_color" />


2、在values中建立styles.xml檔案:定義了統一的style樣式,可以在應用程式中多處使用

<?xml version="1.0" encoding="utf-8"?><resources><style name="AppTheme" parent="android:Theme.Light"></style><style name="content_style">        <item name="android:minHeight">50dp</item>        <item name="android:gravity">left|center</item>        <item name="android:textColor">#000000</item>        <item name="android:textSize">15sp</item>        <item name="android:lineSpacingExtra">2dp</item>        <item name="android:background">#bfbfbf</item>    </style></resources>

在xml中引用:給TextView定義style樣式:@style/content_style

    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="測試style樣式"        style="@style/content_style"/>

3、給Button添加背景圖片:android:background="@drawable/btn_bg1",其中btn_bg1為放在drawable-hdpi、ldpi、mdpi中的圖片資源,hdpi、ldpi、mdpi分別代表高解析度、低解析度、中解析度,是根據android手機的螢幕解析度來決定的;

4、有時候,我們有這樣的需求,按鈕初始化為黃綠色,當我們點擊時候變成紅色背景,類似下面的需求:

初始化:

點擊後:

這時候,我們可以在xml中藉助選取器來定義,selector,我們在res下建立檔案夾drawable,然後編寫我們的selector相應的xml檔案:

<?xml version="1.0" encoding="utf-8"?><selector  xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true" android:drawable="@drawable/btn_bg2" />    <item android:state_focused="true" android:drawable="@drawable/btn_bg4"/>    <item android:drawable="@drawable/btn_bg1" /></selector>

上面定義中:

按鈕按下鎖顯示的圖片:

<item android:state_pressed="true" android:drawable="@drawable/btn_bg2" />

聚焦所顯示的圖片:

<item android:state_focused="true" android:drawable="@drawable/btn_bg4"/>

預設顯示的圖片:

<item android:drawable="@drawable/btn_bg1" />

裡面還有很多的屬性供我們選擇!

我們在xml中引用它:android:background:@drawable/相應的selector檔案名稱.xml

java代碼:R.drawable.相應的selector檔案名稱.xml

5、定義帶圓角的矩形按鈕:同樣是根據selector來選擇,只不過它裡面也可以定義多種shape屬性,shape表示定義多種形狀,用的也很多,可以給我們的介面帶來很豐富的體驗:

<?xml version="1.0" encoding="utf-8"?><selector  xmlns:android="http://schemas.android.com/apk/res/android">  <item android:state_selected="true"><shape>    <gradient android:angle="270" android:startColor="#A5D245"        android:endColor="#99BD4C"/>    <size android:height="60dp" android:width="320dp"/>    <corners android:radius="8dp"/></shape>        </item>  <item android:state_pressed="true"><shape>    <gradient android:angle="270" android:startColor="#A5D245"        android:endColor="#99BD4C"/>    <size android:height="60dp" android:width="320dp"/>    <corners android:radius="8dp"/></shape>        </item>  <item><shape>    <gradient android:angle="270" android:startColor="#A8C3B0"        android:endColor="#C6CFCE"/>    <size android:height="60dp" android:width="320dp"/>    <corners android:radius="8dp"/></shape>        </item></selector>


shape中屬性定義解釋:

<shape>  android:shape=["rectangle" | "oval" | "line" | "ring"]

其中rectagle矩形,oval橢圓,line水平直線,ring環形

<shape>中子節點的常用屬性:

<gradient>  漸層

android:startColor  起始顏色

android:endColor  結束顏色            

android:angle  漸層角度,0從上到下,90表示從左至右,數值為45的整數倍預設為0;

android:type  漸層的樣式 liner線性漸層 radial環形漸層 sweep

<solid >  填充

android:color  填充的顏色

<stroke > 描邊

android:width 描邊的寬度

android:color 描邊的顏色

android:dashWidth 表示'-'橫線的寬度

android:dashGap 表示'-'橫線之間的距離

<corners > 圓角

android:radius  圓角的半徑 值越大角越圓

android:topRightRadius  右上圓角半徑
 
android:bottomLeftRadius 右下圓角角半徑
 
android:topLeftRadius 左上圓角半徑

android:bottomRightRadius 左下圓角半徑

項目中我們還有很多資源需要我們去自己尋找學習和靈活運用!!


android 中怎找到系統本人的資源檔tabWidgetStylexml

如果你說的是“android:attr/tabWidgetStyle”中的這個tabWidgetStyle的話,貌似它並不是在一個單獨的XML檔案中定義的,而是包含在一個總的style XML中。
如果你下載了SDK的話,可以到這個檔案中找找看:platforms\android-XX\data\res\values\styles.xml。
你可能在這個檔案中找不到tabWidgetStyle,而是只能找到Widget.TabWidget。那是因為在themes.xml中,把tabWidgetStyle定義成了Widget.TabWidget。
在platforms\android-xx\data\res下面應該也能找到其它一些系統的資源檔。
 
android中原始資源的幾種存放位置及讀取方法

assets下存放不需要編譯的資源
res 下存放圖片,視頻,文本,需要解析的東西,會在R中產生id
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.