之前略微提到過,可以將常用的屬性定義在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);
}
以上就是所有常見資源的使用。
覺得有用的話記得點個贊吶!!
參考自:尚學堂課