之前略微提到過,可以將常用的屬性定義在res中,顏色、字符、格式等也可以這樣,
常用顏色設定
如在values檔案夾下新建一個顏色檔案,里面這樣寫:
<?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="text_color">#ff0000</color>
<color name="bg_color">#00ff00</color>
</resources>
我們就可以將較常使用的顏色賦予名字,更為方便呼叫
呼叫也分為兩種方式,代碼方式和引數方式
下面我們看呼叫代碼:
public void test(View view){
int color =this.getResources().getColor(R.color.bg_color);//獲得顏色
Toast.makeText(this,""+color,Toast.LENGTH_SHORT).show();//或者在main中TextView 寫android:textColor="@color/text_color"
this.getWindow().setBackgroundDrawableResource(R.color.bg_color);
}
代碼方式我們可以用getResources() 方法獲得我們定義的顏色,并且更改文字或者背景顏色,而引數方式,我們只需像注釋中寫的那樣,增加textColor屬性,
常用字串設定
如同顏色一樣,我們也需要在values檔案夾下的string檔案中自定義我們的字串:
<resources>
<string name="app_name">test_color</string>
<string name="yyj">你好,我是Embers</string>>
</resources>
實際使用的時候,可以通過這樣的格式:
android:text="@string/yyj"
我們就可以給文本賦予提前定義好的值
而在代碼中:
public void test2(View view){
String str =this.getString(R.string.yyj);
Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
Button button=findViewById(R.id.button2);
button.setText(R.string.yyj);
}
我們一般通過R.string.名字來尋找我們的字串,
而上面代碼是兩種方式,第一種是先拿到再使用,第二種是直接使用,這兩種都比較方便,
尺寸資源的設定
也就是我們對于長寬高的定義
px是像素
in是英寸
dp是和密度無關的像素
sp是和精度無關的像素
大家可以自行測驗
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_width">150px</dimen>
<dimen name="text_height">100px</dimen>
<dimen name="button_width">30mm</dimen>
<dimen name="button_height">10mm</dimen>
</resources>
同樣是定義這樣的尺寸屬性,在我們的values檔案夾下,
android:width="@dimen/button_width"
android:height="@dimen/button_height"
直接使用如上所示
代碼使用如下所示
public void test3(View view){
Button button =findViewById(R.id.button3);
float width =this.getResources().getDimension(R.dimen.text_width);
float height =this.getResources().getDimension(R.dimen.button_height);
button.setWidth((int)width);
button.setHeight((int)height);
}
xml資源的設定
我們可以在res檔案夾下定義xml檔案夾,存放我們需要的xml資源

xml檔案中存放需要的資料,例如:
<resources>
<user username="yyj" phone ="110"></user>
<user username="lwl" phone ="120"></user>
</resources>
具體的呼叫如下:
public void test(View view) throws XmlPullParserException, IOException {
String text ="";
XmlResourceParser xrp=this.getResources().getXml(R.xml.users);
while(xrp.getEventType()!=XmlResourceParser.END_DOCUMENT){
if(xrp.getEventType()==XmlResourceParser.START_TAG){
String tagname =xrp.getName();
if(tagname.equals("user")){
String uname=xrp.getAttributeValue(0);
String phone=xrp.getAttributeValue(1);
text+="name:"+uname+";"+phone+";\n";
}
}
xrp.next();
}
TextView textView =(TextView) findViewById(R.id.textView);
textView.setText(text);
}
圖片資源的設定
圖片資源也是存放在res中的drawable中,要特別注意圖片不能以數字開頭,

具體呼叫代碼如下:實作了按不同按鈕更換背景的操作,
public void test1(View view){
Drawable drawable=this.getResources().getDrawable(R.drawable.a1);
this.getWindow().setBackgroundDrawable(drawable);
//也可以在<androidx.里 android:background="@drawable/a1"設定背景
}
public void test2(View view){
Drawable drawable=this.getResources().getDrawable(R.drawable.a2);
this.getWindow().setBackgroundDrawable(drawable);
}
以上就是所有常見資源的使用,
覺得有用的話記得點個贊吶!!
參考自:尚學堂課
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/248120.html
標籤:其他
上一篇:安卓學習日志 — Day06
