1、string文件的使用:
在使用xml中的资源之前,需要在XML文件中定义:
如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<string name="app_name">CameraDemo1</string>
<string name="people_name">Vincent</string>
<string name="address">Earth</string>
</resources>
已定义的资源可以在其他的xml文件或者java文件中使用。
java:
public String getStringResource(){xml:
String s = null;
s = getResources().getString(R.string.people_name);
return s;
}
<Button2、array文件的使用:
android:text="@string/address"
android:id="@+id/address"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
/>
在使用xml中的资源之前,需要在XML文件中定义:如下所示:<?xml version="1.0" encoding="utf-8"?>java:
<resources>
<string-array name="country">
<item>China</item>
<item>Russia</item>
<item>Japan</item>
<item>TailLand</item>
<item>iraq</item>
</string-array>
<integer-array name="number">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</integer-array>
</resources>public int[] getIntResource(){xml:
Resources rs = getResources();
int[] ints = rs.getIntArray(R.array.number);
if (ints != null){
return ints;
}else{
return new int[]{0};
}
}
public String[] getStringArrayResource(){
Resources rs = getResources();
String[] strings = rs.getStringArray(R.array.country);
if (strings != null){
return strings;
}else{
return new String[]{""};
}
}在资源文件中直接引用资源数组的控件,本人很少接触,只了解pinner可以直接使用资源数组。<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/country">
</Spinner>
3 attr文件的使用在使用xml中的资源之前,需要在XML文件中定义:
<?xml version="1.0" encoding="utf-8"?>需要在xml和java文件中同时使用文件引用:一般这种方式的自定义控件属性只能通过使用view的构造方法来获取view的各个属性值。
<resources>
<declare-styleable name="CustomView">
<attr name="size" format="dimension"></attr>
<attr name="color" format="color"></attr>
<attr name="width" format="integer"></attr>
<attr name="picture_resource" format="reference|integer"></attr>
<attr name="view_name" format="string|reference"></attr>
</declare-styleable>
</resources>代码:public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.CustomView);
String name = a.getString(R.styleable.CustomView_view_name);
int color = a.getColor(R.styleable.CustomView_color,1234);
a.recycle();
}xml:<com.example.zhuyuqiang.camerademo1.MyView其实所谓的自定义属性就是讲在xml中所使用的数据出入至View的构造方法中,在构造方法中对自定义的属性进行解析,获取里面的数据,然后根据获取的数据来影响View的外观或者行为。
android:layout_width="match_parent"
android:layout_height="match_parent"
Mycustom:color="12345"
Mycustom:picture_resource="mipmap/ic_launcher"/>