android中colors.xml顏色設(shè)置資源文件的方法
1. 打開res目錄下的values文件夾,雙擊打開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文件中訪問顏色.
上代碼
<?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文件訪問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文件訪問colors資源(藍(lán)色)"
android:id = "@+id/tv4" />
</LinearLayout>
上述代碼中@color/color_xml 為XML文件讀取colors.xml文件中名為color_xml的RGB顏色代碼,并將該顏色以 Text View的形式顯示出來。
3. 在java目錄下的com.example.myapplication包中創(chuàng)建Color_ActivityDemo類


下述代碼演示如何在Java代碼中訪問顏色。
上代碼
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的形式顯示出來。
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android OpenGL ES 實(shí)現(xiàn)抖音傳送帶特效(原理解析)
這篇文章主要介紹了Android OpenGL ES 實(shí)現(xiàn)抖音傳送帶特效,抖音傳送帶特效推出已經(jīng)很長一段時(shí)間了,前面也實(shí)現(xiàn)了下,最近把它整理出來了,如果你有仔細(xì)觀測傳送帶特效,就會(huì)發(fā)現(xiàn)它的實(shí)現(xiàn)原理其實(shí)很簡單,需要的朋友可以參考下2022-07-07
Android實(shí)現(xiàn)相冊中圖片上傳或下載
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)相冊中圖片上傳或下載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
Android中FontMetrics的幾個(gè)屬性全面講解
下面小編就為大家?guī)硪黄狝ndroid中FontMetrics的幾個(gè)屬性全面講解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
Android實(shí)現(xiàn)界面跳轉(zhuǎn)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)界面跳轉(zhuǎn)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
Android內(nèi)置SQLite的使用詳細(xì)介紹
這篇文章主要介紹了Android內(nèi)置SQLite的使用詳細(xì)介紹,文章通過文章展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
Android UI控件之Gallery實(shí)現(xiàn)拖動(dòng)式圖片瀏覽效果
這篇文章主要為大家詳細(xì)介紹了Android UI控件之Gallery實(shí)現(xiàn)拖動(dòng)式圖片瀏覽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android仿微信界面的導(dǎo)航以及右上角菜單欄效果
這篇文章主要介紹了Android仿微信界面的導(dǎo)航以及右上角菜單欄效果,具有很好的參考價(jià)值,希望對大家有所幫助,一起跟隨小編過來看看吧2018-05-05

