android strings, colors and sizes

First look at the sample





Demo download
Finally , the above examples are all sourced from Android worry-free, please go to the application treasure or pea pod to download: http://android.myapp.com/myapp/detail.htm?apkName=com.shandong.mm.androidstudy , the source code example document is exhausted.

1 String resource
file path: res/values/strings.xml
1.1 String resource definition example
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello! </string>
</resources>
1.2 String resource call Call string resource
in Layout XML:
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />
Get string resources in Activity:
this.getString(R.string.


Get a string resource from Application:
application.getString(R.string.hello)
2 Color resource
2.1 Use the constant of the Color class
int color = Color.BLUE; // Create a blue color using the color provided by Android
int color = Color. RED; int color = Color.WHITE;
2.2 is constructed by ARGB,
int color = Color.argb ( 127, 255, 0, 255 ); // semi-transparent purple
where the first parameter means transparent, 0 means fully transparent, 255 (ff) means completely opaque; the last three digits represent the RGB value respectively.
2.3 Use XML resource files to define colors 
. This method has good scalability and is easy to modify and share. For example, create a color.xml in the values ​​directory: 
<?xml version=” 1.0” encoding=”utf -8”> 
<resources> 
< color name=”mycolor”> #7fff00ff</color> 
</resources>
defines a color named mycolor, which can be obtained by referencing mycolor elsewhere, such as in the textView definition: 
android:textColor= "@drawable/mycolor"
2.4 getColor
In Java code, you can use getColor in the ResourceManager class to get the color: int color = getResources().getColor(R.color.mycolor); 
This is the same as the value obtained by the second method. The getResources() method returns the current Activity Activity's ResourceManager class instance. 
Note: The XML definition method accepts both 6-bit and 8-bit representations, and the beginning must be #, and the first two digits are transparent when 8-bit is defined. 4. Define the color value directly, such as: int color = 0xff00ff00;
this method must start with 0x instead of the # that we commonly use. Unlike method 3, the value must also be represented in 8 bits, and a 6-bit color representation is not accepted. Group 0x|ff|ff00ff, 0x is the mark representing the color integer, ff is the transparency, and ff00ff is the RGB color value. 
Add a little background image setting in Android layout (edit LinearLayout): 
* You can use solid color: android:background="@drawable/mycolor" (the color defined in the XML resource file)
* You can also use image: android:background="@ drawable/bg" (you need to copy a picture named bg.jpg or png to the res/drawable-hdpi directory)
Color classification:
2.5 System colors
Android built-in colors, such as the colors defined in system resources, include the following :
BLACK (black), BLUE (blue), CYAN (cyan), GRAY (gray), GREEN (green), RED (red), WRITE (white), YELLOW (yellow), etc.
Of course, android's android.graphics.Color also provides a static method for constructing custom colors. Use of
system colors
① Set
Button directly in Java code btn = (Button) findViewById(R.id.btn);
        btn.setBackgroundColor(Color.BLUE );

Of course, you can also get the system color and then set it:
int getcolor = Resources.getSystem().getColor(android.R.color.holo_green_light);
        Button btn = (Button) findViewById(R.id.btn);
        btn. setBackgroundColor(getcolor); ②Use 2.6 custom color
in the layout file The definition of color value is defined by transparency alpha and RGB (red, green and blue) three primary colors, starting with "#", followed by: transparency-red-green -Blue eg: #RGB #ARGB #RRGGBB #AARRGGBB And we use the last two custom colors most often: 2.6.1 Use directly in the xml file Of course, you can also create a new one in the res/values ​​directory The color.xml file, give a name to the color you specify, so that you can use the custom color directly according to the name when you need it







<!--?xml version=1.0 encoding=utf-8?-->
<resources>
    <color name="mycolor">#748751</color>
</resources>
2.6.2 Use in Java code
if in The custom color is already defined in res, just call it directly in java code:
int mycolor = getResources().getColor(R.color.mycolor);
        Button btn = (Button) findViewById(R.id.btn );
        btn.setBackgroundColor(mycolor);
If it is defined directly in the java code, pay attention here, the transparency cannot be omitted! ! ! Like this 0xFF080287, the preceding 0x represents hexadecimal:
int mycolor = 0xff123456;
        Button btn = (Button) findViewById(R.id.btn);
        btn.setBackgroundColor(mycolor);
2.7 Use the static method argb to set the color:
Button btn = (Button) findViewById(R.id.btn);
        btn.setBackgroundColor(Color.argb(0xff, 0x00, 0x00, 0x00));
The parameters of the argb() method are the transparency, the size of red, green, and blue, which can be understood as the concentration, and the combination here is white

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326566940&siteId=291194637