android中colors.xml顏色設(shè)置資源文件的方法
1. 打開(kāi)res目錄下的values文件夾,雙擊打開(kāi)colors.xml文件進(jìn)行編輯
上代碼
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#008577</color> <color name="colorPrimaryDark">#00574B</color> <color name="colorAccent">#D81B60</color> <color name="color_xml">#ff0000</color> <color name="color_java">#0000ff</color> </resources>
2. 在res目錄下的layout文件夾創(chuàng)建color_layout.xml文件
下述代碼演示如何在XML文件中訪問(wèn)顏色.
上代碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text = "XML文件訪問(wèn)colors資源(紅色)" android:id = "@+id/tv3" android:textColor="@color/color_xml" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text = "Java文件訪問(wèn)colors資源(藍(lán)色)" android:id = "@+id/tv4" /> </LinearLayout>
上述代碼中@color/color_xml 為XML文件讀取colors.xml文件中名為color_xml的RGB顏色代碼,并將該顏色以 Text View的形式顯示出來(lái)。
3. 在java目錄下的com.example.myapplication包中創(chuàng)建Color_ActivityDemo類
下述代碼演示如何在Java代碼中訪問(wèn)顏色。
上代碼
package com.example.myapplication; import android.os.Bundle; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class Color_ActivityDemo extends AppCompatActivity { TextView tv4; public void onCreate(Bundle saveInstanceState) { super.onCreate(saveInstanceState); setContentView(R.layout.color_layout); tv4 = (TextView) findViewById(R.id.tv4); tv4.setTextColor(getResources().getColor(R.color.color_java)); } }
上述代碼中g(shù)etResources ().getColor(R.color.color _java)為Java代碼讀取colors.xml文件中名為color_java的 RGB顏色代碼,并將該顏色以TextView的形式顯示出來(lái)。
4. 在AndroidMainfest.xml文件添加Color_ActivityDemo.java的列表
5. 運(yùn)行Color_ActivityDemo.java
運(yùn)行結(jié)果
到此這篇關(guān)于android中colors.xml顏色設(shè)置資源文件的文章就介紹到這了,更多相關(guān)android中colors.xml顏色內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android OpenGL ES 實(shí)現(xiàn)抖音傳送帶特效(原理解析)
這篇文章主要介紹了Android OpenGL ES 實(shí)現(xiàn)抖音傳送帶特效,抖音傳送帶特效推出已經(jīng)很長(zhǎng)一段時(shí)間了,前面也實(shí)現(xiàn)了下,最近把它整理出來(lái)了,如果你有仔細(xì)觀測(cè)傳送帶特效,就會(huì)發(fā)現(xiàn)它的實(shí)現(xiàn)原理其實(shí)很簡(jiǎn)單,需要的朋友可以參考下2022-07-07Android實(shí)現(xiàn)相冊(cè)中圖片上傳或下載
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)相冊(cè)中圖片上傳或下載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05Android中FontMetrics的幾個(gè)屬性全面講解
下面小編就為大家?guī)?lái)一篇Android中FontMetrics的幾個(gè)屬性全面講解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11Android實(shí)現(xiàn)界面跳轉(zhuǎn)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)界面跳轉(zhuǎn)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09Android內(nèi)置SQLite的使用詳細(xì)介紹
這篇文章主要介紹了Android內(nèi)置SQLite的使用詳細(xì)介紹,文章通過(guò)文章展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09Android UI控件之Gallery實(shí)現(xiàn)拖動(dòng)式圖片瀏覽效果
這篇文章主要為大家詳細(xì)介紹了Android UI控件之Gallery實(shí)現(xiàn)拖動(dòng)式圖片瀏覽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android仿微信界面的導(dǎo)航以及右上角菜單欄效果
這篇文章主要介紹了Android仿微信界面的導(dǎo)航以及右上角菜單欄效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,一起跟隨小編過(guò)來(lái)看看吧2018-05-05