Follow Me Android Seven Resource Files

Video lesson: https://edu.csdn.net/course/play/7621

Contents of this chapter

Section 1 String, Color and Size Resources
Section 2 Array Resources
Section 3 Drawable Resources
Section 4 Layout Resources
Section 5 Style and Theme Resources

Objective of this chapter 



Familiar with the use of string, color and size resources.
Familiar with the usage of array resources.
Master the usage of various Drawable resources.
Master the usage of layout resources.
Master the usage of style and theme resources.

String constant resource 



Use string constant resources. When there are string constants that need to be used in the program, you should define the string constant resources, which are defined in res/values/string.xml.


<?xml version="1.0" encoding="utf-8"?><resources><string name=“okLabel”>确定</string></resources>
Use in XML layout





<Button … android:text=“@string/okLabel” />
Use in java code 
Button okBtn = (Button)findViewById(R.id.okBtn);okBtn.setText(getString(R.string.okLabel));

颜色值通过RGB(红、绿、蓝)三原色和一个透明度(Alpha)值表示。它必须以“#”开头,后面接Alpha-Red-Green-Blue形式的内容。其中,Alpha值可以省略,如果省略,表示颜色默认是完全不透明的#RGB:使用红、绿、蓝三原色的值来表示颜色,其中,红、绿和蓝采用0~f来表示。例如,要表示红色,可以使用#f00。#ARGB:使用透明度以及红、绿、蓝三原色来表示颜色,其中,透明度、红、绿和蓝均采用0~f来表示。例如,要表示半透明的红色,可以使用#6f00。#RRGGBB:使用红、绿、蓝三原色的值来表示颜色,与#RGB不同的是,这里的红、绿和蓝使用00—ff来表示。例如,要表示蓝色,可以使用#0000ff。#AARRGGBB:使用透明度以及红、绿、蓝三原色来表示颜色,其中,透明度、红、绿均采用00-ff来表示。例如,要表示半透明的绿色,可以使用#6600ff00。


颜色常量资源的使用


颜色的定义是通过RGB三色和一个alpha值来定义的,#RGB、#ARGB、#RRGGBB、#AARRGGBB,在资源文件中定义颜色,一般在res/values下建议colors.xml文件,定义颜色如下:


<resources>
        <color name=“red”>#ff0000</color>
</resources>

在代码中使用颜色



int color = Resources.getSystem().getColor(R.color.red);btn.setBackgroundColor(color);

在布局文件中使用颜色




android:background="@color/red"


Unit of measure

The measurement unit
px (pixel) in the attribute has the same display effect on different devices
in (inch) length unit
mm (millimeter) length unit
pt (pound) 1/72 inch
dp (pixels independent of density)
An abstraction based on screen density The unit is
on a monitor with 160 dots per inch, 1dp=1px
dip (same as dp)
sp (pixels not related to scale)
is similar to dp, but it can be scaled according to the user font size. It is
recommended to use sp as the font size unit

Use the size constant resource, define some fixed size information in the size constant resource, such as: interval, defined in res/values/dimens.xml.



<resources><!-- Default screen margins, per the Android Design guidelines. --><dimen name="horizontal_margin">16dp</dimen><dimen name="vertical_margin">16dp</dimen></resources>
Use in XML layout 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:paddingBottom="@dimen/vertical_margin"android:paddingLeft="@dimen/horizontal_margin“ …>
Use in code 
int margin = getResources (). getDimension (R.dimen. horizontal_margin);



Array resource 


The file is located in the res\values ​​directory, and the root element is the <resources></resources> tag. In this element, the following 3 sub-elements are included. The
<array> sub-elements are used to define common types of arrays.
<integer-array> sub-element: used to define an integer array.
<string-array> sub-element: used to define string array

Use array constant resources. When an array with fixed content needs to be used in the program, you can define an array constant resource, which is defined in res/values/arrays.xml.


<?xml version="1.0" encoding="utf-8"?><resources><string-array name="booktype"><item>语言类</item><item>工具类</item></string-array></resources>


In the code, you can use the ID directly where the ID can be used, and use the method call where it is not convenient.



String[] array = getResources().getStringArray(R.array.booktype);



.9.png images have the ability to adjust the size adaptively in the android environment

Shape can be used to define the element attributes of various UIs. Usually, a corresponding XML file is created under res/drawable to define shape, and solid is used to define solid filling, such as:

<solid android:color="#ff9d77"/>

gradient用于定义渐变色,比如:


<gradient  android:startColor="#eb7720"  android:endColor="#ddbda5" android:angle="270” />


stroke用于定义描边 



<strokeandroid:width="2dp"android:color="#dcdcdc" />




布局资源文件 

布局资源文件放置在res\layout目录下,布局资源文件的根元素通常是各种布管理器,在该布局管理器中,通常是各种View组件或是嵌套的其他布局管理器。
加载布局  

setContentView(R.layout.main);

引入布局




<include layout=“@layout/image”/>



样式与主题概述 

Android中的视图可以通过样式和主题进行外观的定制,通常在res/values下建立styles.xml来定义样式和主题,主题和样式都在使用style标签进行定义,通过name属性确定一个唯一的名字,通过parent属性指定用于继承的父类样式,通过子标签item定义各种样式的内容,示例如下:

使用样式来改变单个视图外观,比如:TextView

<resources xmlns:android="http://schemas.android.com/apk/res/android"><style name="mystyle"><item name="android:textColor">#FF0000</item><item name="android:textSize">20sp</item><item name="android:background">@drawable/shap_background</item></style></resources>


首先在res/values下建立styles.xml来定义样式
<resources> <style name="textViewStyle” parent="textViewStyle"> <item name="android:textColor">#FF0000</item> <item name="android:textSize">20sp</item> </style><style name="testTheme"> <item name="android:windowNoTitle">true</item>   <item name="android:windowFullscreen">?android:windowNoTitle</item>   </style> </resources><strong></strong>

在TextView中使用样式 



<TextView android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/hello_world”  style="@style/mystyle” />

使用主题来改变整个应用的外观,当把样式用来改变应用或者Activity的时候就叫做主题了,首先在res/values下建立styles.xml来定义样式 



<resources xmlns:android="http://schemas.android.com/apk/res/android"><style name="mytheme"><item name="android:windowNoTitle">true</item><item name="android:windowFullscreen">?android:windowNoTitle</item></style></resources>

在Application中使用主题 



<applicationandroid:allowBackup="true“ android:icon="@drawable/ic_launcher"android:label="@string/app_name”  android:theme="@style/mytheme" >


Guess you like

Origin blog.51cto.com/2096101/2588818