UI系列__區分style selector在drawable文件夾下還是在value下

 Values文件夾      

   · arrays.xml:定義數組數據;
   · colors.xml:定義表示顏色的數據;
   · dimens.xml:定義尺度,可以使用Resources.getDimension()獲得這些資源;
   · strings.xml:定義字符串,可以使用Resources.getString()或Resources.getText()方法獲得這些資源;
   · styles.xml:定義顯示的樣式文件;

        都是<resources />包圍

        dimens      主要定義文字大小sp,組件的height width
        Styles      樣式 如:Dialog的無標題 透明 什麼  如:TextView的文字大小 寬高都可以  

res  /values  /dimens.xml
<resources>
    <dimen name="textSize_TextView">30sp</dimen>
    <dimen name="width_EditText">400dp</dimen>
    <dimen name="height_EditText">100dp</dimen>
</resources>

res  /values  /colors.xml
<resources>
    <color name="blue">#3399cc</color>
    <color name="red">#cc3300</color>
</resources>

res  /values  /styles.xml
    <style name="editText_style">
        <item name="android:textColor">@color/blue</item>
        <item name="android:gravity">center</item>
        <item name="android:text">"樣式"</item>
        <item name="android:textSize">18sp</item>
        <item name="android:layout_height">200dp</item>
        <item name="android:layout_width">500dp</item>
        <item name="android:layout_margin">10dp</item>
    </style>

main .xml
    <!-- dimen和color的使用 -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView  textSize=30sp"
        android:textSize="@dimen/textSize_TextView"
        android:layout_margin="10dp"
         />
    <EditText 
        android:layout_width="@dimen/width_EditText"
        android:layout_height="@dimen/height_EditText"
        android:hint="EditText width:500  height:200"
        android:textColorHint="@color/red"
        android:layout_margin="10dp"
        />
    <!-- style和selector的使用 -->
   <EditText         
        style="@style/editText_style"
        android:background="@drawable/edittext_selector"
       />  
   
main.xml中使用到的style樣式 是在values文件夾下
                     而用到的background則是在drawable文件夾下 
   <selector />  <shape  /> 這些都在drawable文件夾下

   drawable文件夾

                   一般用來放置一些xml文件 如樣式,常被android:background="@drawable/xxx"訪問

drawable /bg_edittext_normal
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:radius="3dp"  />
    <solid   android:color="#FFFFFF"  />
    <stroke  android:width="2dp"
             android:color="#ffcc00"/>
</shape>

drawable /bg_edittext_focused
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:radius="3dp"/>
    <solid   android:color="#FFFFFF"/>
    <stroke  android:width="2dp"
             android:color="@color/blue"  />
</shape>

drawable /edittext_selector
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
     <item android:state_focused="false" android:drawable="@drawable/bg_edittext_normal"></item>
     <item android:state_focused="true" android:drawable="@drawable/bg_edittext_focused"></item>    
</selector>
     <!--  上面寫成android:background="@drawable/bg_edittext_normal"  編譯通過 但一運行就退出 
            記住了 是 android:drawable 不是android:background -->


      截圖:

     

EditText獲取焦點 通過selector 變色

          

        
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章