Android樣式主題及自定義屬性


一、 Selector——圖形、顏色選擇器
語法
<selector>
  <item android:drawable=“drawableResA” android:state_xxxxx=“true”/>
<item android:drawable=“drawableResB” android:state_xxxxx=“false”/>
<selector>
<selector>
  <item android:color=“drawableResA” android:state_xxxxx=“true”/>
  <item android:color=“drawableResB” android:state_xxxxx=“false”/>
<selector>
例:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 
<!-- android:state_pressed=""按下-->
<item android:drawable="@mipmap/bar_1_select" android:state_checked="true"/>
<item android:drawable="@mipmap/bar_1_unselect" android:state_checked="false"/>
</selector效果圖:
 
二、 常用的styles.xml修改和設置
<resources>

    <style name="sss" parent="@android:style/Theme.Holo.Light.Dialog"></style>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <!--標題欄-->
        <item name="colorPrimary">@color/colorPrimary</item>
        <!--狀態欄-->
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <!--系統中,子空間,復選框,單選按鈕,選中時的顏色-->
        <item name="colorAccent">@color/colorAccent</item>


        <!--窗體的背景色-->
        <item name="android:windowBackground">@android:color/white</item>

        <!--窗體無標題欄-->
        <item name="windowNoTitle">true</item>
        <!--無actionbar-->
        <item name="windowActionBar">false</item>
        <!--取消狀態欄,也就是全屏顯示-->
        <item name="android:windowFullscreen">true</item>
    </style>


    <!--需要注意的是,不能有空格,值不用引號-->
    <style name="bottom_bar_rb">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_marginTop">5dp</item>
        <item name="android:layout_marginBottom">5dp</item>
        <item name="android:gravity">center</item>
        <item name="android:layout_weight">1</item>
        <item name="android:button">@null</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:textSize">14sp</item>
        <item name="android:textColor">@color/selector_bar_font_1</item>
    </style>
</resources>

 

二、 常用的styles.xml修改和設置
Style——樣式
樣式就是各種屬性的集合。當控件需要使用這些屬性時,就可以直接使用這個集合來獲得這些屬性的
樣式分為對控件的樣式和對整個應用或Activity的樣式,對整個應用或Activity的樣式我們有稱其為主題(Theme)
語法:
聲明:
在values文件夾中
<style name=“styleName” parent=“extends”>
<item name=“paramsName”>value</item>
</style>
使用:
控件的style屬性引用  style=“styleName”(布局頁面)
三、 Attribute
Attribute是位於 values文件夾下的一種android資源,通常使用名稱為attrs的xml資源文件來聲明。可以作為自定義控件的自定義屬性,也可以作為根據主題自動選擇值的一種資源值。
 
用法:
作為可變資源
聲明
<resource>
  <attr name=“attr-name” format=“format-type”/>
</resource>
賦值
<style name=“xx”>
  <item name=“attr-name”>value</item>
</style>
 
例:values文件夾下的,attrs.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--注意不能和已有的重名,不會會有異常-->
<attr name="bg" format="color"/>
<!--格式為引用-->
<attr name="radioButtonStyle" format="reference"/>
</resources>
引用的時候:
android:background="?attr/bg">
具體代碼,Layoutwork
四、 Shape——圖形
用來實現一些簡單的圖片,可控制圖片的圖形、顏色、邊框圓角等。占用空間小,還可以適應不同大小的尺寸,不發生形變。
用法:
聲明在drawable文件夾下
<shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape=["rectangle" | "oval" | "line" | "ring"]>
</shape>
shape  圖形  rectangle  矩形  line  線  oval  橢圓  ring  環
  
具體的案例:
一、矩形代碼:
<?xml version="1.0" encoding="utf-8"?>
<!--默認矩形-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">
 
 <!--純色-->
 <solid android:color="@color/colorAccent"/>
 
 <!--圓角-->
 <!-- android:radius="100dp"四個角都是圓角-->
 <corners
 android:bottomLeftRadius="100dp"
 android:topRightRadius="100dp"/>
 
 <!--邊框-->
 <!--android:dashWidth="50dp線段長-->
 <!--android:dashGap="20dp"線段之間的間隔-->
 <stroke
 android:width="10dp"
 android:color="@color/colorPrimary"
 android:dashWidth="50dp"
 android:dashGap="20dp"/>
 <!--大小-->
 <size
 android:width="200dp"
 android:height="200dp"/>
 
 <padding
 android:left="20dp"
 android:top="20dp"
 android:bottom="20dp"
 android:right="20dp"/>
 <!--漸變-->
 <!--默認左右漸變-->
 <!--android:centerColor="@color/colorPrimary" 三色漸變-->
 <!--漸變類型,默認為linear android:type="linear"-->
 <!--注意,這個屬性的值只能是45的倍數,並且只能在linear中使用-->
 <!--<gradient-->
 <!--android:centerColor="@color/colorPrimary"-->
 <!--android:startColor="#fff"-->
 <!--android:endColor="@color/colorAccent"-->
 <!--android:type="linear"-->
 <!--android:angle="-90"/>-->
 
 
 <!--android:type="radial" 徑向漸變-->
 <!--注意必須設置徑向漸變半徑-->
 <!--<gradient-->
 <!--android:centerColor="@color/colorPrimary"-->
 <!--android:startColor="#fff"-->
 <!--android:endColor="@color/colorAccent"-->
 <!--android:type="radial"-->
 <!--android:gradientRadius="200"-->
 <!--android:centerX="0.5"-->
 <!--android:centerY="0.3"/>-->
 
 <!--掃描線-->
 <!--也可以設置中心點,默認是0.5XY-->
 <gradient
 android:centerColor="@color/colorPrimary"
 android:startColor="#fff"
 android:endColor="@color/colorerAccent"
 android:type="sweep"
 android:centerX="0.5"
 android:centerY="0.5"/>
 
</shape>

 


 
二、線代碼
 
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="line">
 <stroke
 android:width="3dp"
 android:color="@color/colorPrimary"
 android:dashGap="20dp"
 android:dashWidth="50dp"/>
</shape>

 


三、橢圓代碼                                          
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 
 android:shape="oval">
 <!--這里邊的屬性都可以使用-->
 <solid android:color="@color/colorAccent" />
 <!--寬高相等,會變成圓形-->
 <size
 android:width="50dp"
 android:height="200dp" />
</shape>

 


四、圓環代碼                                        
 
<!--環形-->
 <!--固定值,好處,固定大小不變,但是不能根據控件大小變化-->
<!--android:innerRadius="" 內環半徑
 
 android:thickness="" 環厚度-->
<!--**** android:useLevel="false"必須為false,
默認為true,為false時顯示*****-->
<!--比例
 android:innerRadiusRatio=""
 android:thicknessRatio=""-->
<!--控件的寬度/5,內環的半徑 控件的寬度/7,環厚度-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="ring"
 android:innerRadiusRatio="5"
 android:thicknessRatio="7"
 android:useLevel="false">
 <solid
 android:color="@color/colorAccent"/>
</shape>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM