溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用XML資源文件來(lái)定義顏色,樣式等

發(fā)布時(shí)間:2021-03-12 11:41:21 來(lái)源:億速云 閱讀:663 作者:小新 欄目:編程語(yǔ)言

小編給大家分享一下如何使用XML資源文件來(lái)定義顏色,樣式等,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

1、使用XML資源文件來(lái)定義顏色該方法擴(kuò)展性好,便于修改和共享,如在values目錄下創(chuàng)建一個(gè)color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="mycolor">#ffb52121</color> 
</resources>

定義了一個(gè)名為mycolor的顏色,在別的地方就可以通過(guò)引用mycolor來(lái)獲取該顏色值,如

textView定義中:

Android:textColor="@drawable/mycolor"

2、使用XML資源文件來(lái)定義樣式在res/drawable中定義一個(gè)shape.xml文件

實(shí)例代碼:

<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  
    <corners android:radius="5dp" />  
  
    <size  
        android:height="30dp"  
        android:width="20dp" />  
  
    <gradient   
        android:startColor="#9933cc"  
        android:endColor="#aa66cc"  
        android:angle="90"  
        />  
      
    <padding android:left="5dp"  
        android:right="5dp"  
        android:top="5dp"  
        android:bottom="5dp"/>  
  
</shape>  
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
 
    <!-- 填充的顏色:這里設(shè)置背景透明 -->
    <solid android:color="@android:color/transparent" />
    <!-- 邊框的顏色 :不能和窗口背景色一樣-->
    <stroke
        android:width="3dp"
        android:color="#ffffff" />
    <!-- 設(shè)置按鈕的四個(gè)角為弧形 -->
    <!-- android:radius 弧形的半徑 -->
    <corners android:radius="5dip" />
 
    <!-- padding:Button里面的文字與Button邊界的間隔 -->
    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp" />
</shape>

在你的Activity的xml(比如activity_main.xml)中定義按鈕

<Button
    android:id="@+id/roundButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/shape"
    android:text=" 圓角按鈕 " />

Shape的屬性:

solid

描述:內(nèi)部填充

屬性:android:color 填充顏色

size

描述:大小

屬性:

     android:width 寬
                 android:height 高

gradient

描述:漸變色

屬性:

     android:startColor漸變起始顏色
                   android:endColor漸變結(jié)束顏色
                   android:centerColor漸變中間顏色
                   android:angle 漸變的角度,angle=0時(shí),漸變色是從左向右,然后逆時(shí)針?lè)较蜣D(zhuǎn);當(dāng)angle=90時(shí),漸變色從上往下。angle必然是45的倍數(shù)
                   android:type 漸變類型:linear(線性)、radial(放射性,以開(kāi)始色為中心)、sweep(掃描線式漸變)
                   android:userLevel如果要使用LevelListDrawable對(duì)象,就要設(shè)置為true。設(shè)置true無(wú)漸變色,false有漸變色
                   android:grdientRadial漸變半徑,只有設(shè)置type為radial時(shí),此值才生效
                   android:centerX 漸變中心X點(diǎn)坐標(biāo)的相對(duì)位置
                   android:centerY 漸變中心Y點(diǎn)坐標(biāo)的相對(duì)位置

stroke

描述:描邊

屬性:

     android:width 描邊寬度
                   android:color 描邊顏色
                   android:dashwidth描邊樣式為虛線時(shí)的寬度,值為0時(shí)為實(shí)線,值大于0時(shí)為虛線
                   android:dashGap 描邊為虛線時(shí),虛線之間的間隔

corners

描述:圓角

屬性:

       android:radius 四個(gè)角半徑值
                   android:topLeftRadius左上角半徑值
                   android:topRightRadius右上角半徑值
                   android:bottomLeftRadius右下角半徑值
                   android:bottomRightRadius左下角半徑值

padding

描述:內(nèi)邊距

屬性:

 android:left 左內(nèi)邊距
                   android:right 右內(nèi)邊距
                   android:top 上內(nèi)邊距
                   android:bottom 下內(nèi)邊距:

看完了這篇文章,相信你對(duì)“如何使用XML資源文件來(lái)定義顏色,樣式等”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

xml
AI