资源文件初学

来源:互联网 发布:韩国代购的软件 编辑:程序博客网 时间:2024/05/18 02:47

1)同一个包具体含义

classAdir1/dir2/dir3下,而classBdir1/dir2下,虽然都是dir2下,但是不是同一个包。判断是否在同一个包下最好的方法是查看源文件package关键字后面跟的包名是否完全一致。 

(2)string.xml中需要注意的地方

<?xmlversion="1.0" encoding="utf-8"?>

<resources>


<stringname="app_name">ResTest(August)</string>

<stringname="hello_world1">Hello \'world!\'</string> <!-- 单双引号要加反斜杠,否则不显示-->

<stringname="hello_world2"> Hello \"world!\" </string> <!-- 前后加空格与不加空格实测效果相同(至少TextView-->

<stringname="hello_world3">Hello &amp;world!&amp;</string> <!-- &amp; 相当于& -->

<stringname="hello_world4">Hello &lt;world!&lt;</string> <!-- &lt; 相当于< -->

<stringname="action_settings">Settings</string>

<!--

&lt; < 小于

&gt; > 大于

&amp; & 和号

&apos; ' 单引号

&quot; " 引号


注释:在XML中,只有字符"<""&"确实是非法的。大于号是合法的,但是用实体引用来代替它是一个好习惯。

XML的属性值须加引号。

XML标签对大小写敏感。

XML中,省略关闭标签是非法的。

-->

</resources> 

(3)arrays.xml中需要注意的地方

首先是代码中获取arrays.xml的资源

int[]myInt;

String[]myString;

myInt=getResources().getIntArray(R.array.myIntArray);

myString=getResources().getStringArray(R.array.myStringArray);


<?xmlversion="1.0" encoding="utf-8"?>

<resources>

<integer-array name="myIntArray" >

<item>1</item>

<item>2</item>

<item>3</item>

<item>4</item>

</integer-array>

<string-arrayname="myStringArray" >

<item> dota</item> <!-- 前后加空格依然和没加空格一样-->

<item>dota2</item>

<item>dota3</item>

<item>dota4</item>

</string-array>


</resources>

(4)res/values下的资源比如数组只能放在arrays.xml中吗?可不可以放在array.xml或者strings.xml中?

只要是放在res/values中就行,与文件名无关,只要标签名对了就行。

(5)colors.xml的使用方法

res/values/colors.xml(同上也可以放在res/values下任何xml文件里,只要标签对就可以找到)

<?xmlversion="1.0" encoding="utf-8"?>

<resources>

<colorname="white">#ffffff</color>

<colorname="black">#000000</color>

</resources>

main.xml文件(layout下)的组件中用android:textColor="@color/white"使用。(此处是color不是colors因为是根据标签找的)

好处是可以不用在布局文件中用#XXXXXX去设置颜色(不知道具体是啥颜色)

代码中获取colors.xml的资源

Resourcesresources =YourActivity.this.getResources();

Drawabledrawable = resources.getDrawable(R.color.black); 

(6)dimens.xml使用方法

color.xml相同,一般也是布局文件中引用,android:paddingBottom="@dimen/activity_vertical_margin"

(7)styles.xml使用方法以及注意

<stylename="SecurityPreferenceButtonContainer"parent="@android:style/Widget.Material.Light.SegmentedButton">

<itemname="android:layout_width">match_parent</item>

<itemname="android:layout_height">wrap_content</item>

<itemname="android:weightSum">2</item>

<itemname="android:dividerPadding">8dip</item>

</style>

一般也是布局文件中引用,如style="@style/SecurityPreferenceButtonContainer"

注意style有个parent=""。样式资源。参考从中这种风格要继承样式属性的样式。

注意我们用了@符号和?符号来应用资源。@符号表明了我们应用的资源是前边定义过的(或者在前一个项目中或者在Android框架中)。问号?表明了我们引用的资源的值在当前的主题当中定义过。

<?xmlversion="1.0" encoding="utf-8"?>

<resources>

<stylename="CustomTheme">

<itemname="android:windowNoTitle">true</item>

<itemname="windowFrame">@drawable/screen_frame</item>

<itemname="windowBackground">@drawable/screen_background_white</item>

<itemname="panelForegroundColor">#FF000000</item>

<itemname="panelBackgroundColor">#FFFFFFFF</item>

<itemname="panelTextColor">?panelForegroundColor</item>

<itemname="panelTextSize">14</item>

<itemname="menuItemTextColor">?panelTextColor</item>

<itemname="menuItemTextSize">?panelTextSize</item>

</style>

</resources>

(8)themes.xml使用方法

themes.xml的标签和styles.xml是一样的。

AndroidManifest.xml中使用。

styles.xml不同的是你通过在AndroidManifest中定义的<application><activity>元素将主题添加到整个程序或者某个Activity,但是主题是不能应用在某一个单独的View里。

<applicationandroid:theme="@style/CustomTheme"></application>

<activityandroid:theme="@android:style/Theme.Dialog"></activity>


在代码中使用。

protectedvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

...

setTheme(android.R.style.Theme_Light);

setContentView(R.layout.linear_layout_3);

}

(9)Android支持哪些图片类型资源

例如jpgpngbmpgif(不包括动画gif)9.png

(10)自定义按钮

res/drawable目录下新建xml文件

<?xmlversion="1.0" encoding="utf-8"?>

<selectorxmlns:android="http://schemas.android.com/apk/res/android">

<item

android:state_window_focused="false"

android:drawable="@drawable/rrr"/>

<item

android:state_pressed="true"

android:drawable="@drawable/rr"/>

<item

android:drawable="@drawable/pp"/>

</selector>


<!--

<selector></selector>标签对应StateListDrawable

android:state_checked:是否被选中。这种写法一般会用在ToggleButton或者Checkbox的控件里

android:state_window_focused:应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了

android:state_pressed:是否按下,如一个按钮触摸或者点击。

系统按照从上到下的顺序查找的。默认的item不附带状态,放在最后,可以匹配View的任何状态。

-->

然后layout文件的button组件中引用

<Button

android:layout_width="200dp"

android:layout_height="200dp"

android:background="@drawable/button"/>

button的大小会随着设置的layout_width,layout_height自行缩放。

如果buttonandroid:text内容较多,也会对应缩放。

<ImageButton

android:layout_width="match_parent"

android:layout_height="120dp"

android:src="@drawable/button"

android:background="@drawable/button"

android:scaleType="center"

/>

android:src对应的是src是图片内容(前景),bg是背景,可以同时使用。

background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸。

scaleType只对src起作用。

ButtonImageButton默认有个灰色背景,android:background设置后就替代掉了。

Imagebutton继承Imageview,就是用一个图标代表了一些文字,它没Android:text属性。

Button继承Textview,所以TextView的一些属性也适用于Button控件。

ImageView设置android:src="@drawable/button"

android:background="@drawable/button"

效果是一样的,而且点击没有反应,图片不会切换(切换前后台的时候有反应)。

所以自定义按钮优先选择ButtonImageButton

(8)tip

所有的组件(包括布局)都要定义android:layout_width,android:layout_height两项属性。


原创粉丝点击