Use the andrid resource file

Source: Internet
Author: User
1 use the color resource file to first define the color resource file colors. xml. The Code is as follows <? Xmlversion = "1.0" encoding = "UTF-8"?> <Resources> <colorname = "red_bg"> # f00 </color> <colorname = "blue_text"> # 0000ff </color> </resources> and then reference color resources in activity_main.xml, the Code is as follows: <textview Android: textcolor = "@ color/blue_text" // set the font color of the view. Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "@ string/hello_world"/> then set the view background color protected void oncreate (bundle savedinstancestate) on the mainactivity class {super. oncreate (savedinstance State); setcontentview (R. layout. activity_main); getwindow (). setbackgrounddrawableresource (R. color. red_bg); // set the view background color} 2 use the string (string) constant string reference format: "@ string/string Resource Name" 3 Use the size (dimen) resource PX pixels real pixels on the screen indicate in size based on the screen physical size DP and density-independent precision abstract units relative to the screen physical density SP and precision-independent pixels are similar to DP to obtain resources: resources r = getresources (); dimens. XML Code <resources> <! -- Default screen margins, per the android design guidelines. --> <dimenname = "strong"> 16dp </dimen> <dimenname = "activity_vertical_margin"> 16dp </dimen> <dimenname = "text_width"> 150px </dimen> // text width <dimenname = "text_height"> 100px </dimen> // text height <dimenname = "btn_width"> 30mm </dimen> // button width <dimenname = "btn_height ""> 10mm </dimen> // button height </resources> Use dimen resources // use findviedbyid to obtain the button instance mybutton = (Button) findviewbyid (R. id. testdimenbutton); // obtain the resource instance resources r = getresources (); // use the getdimension method to obtain the size value float btn_h = R. getdimension (R. dimen. btn_height); float btn_w = R. getdimension (R. dimen. btn_width); // set the width and height of the button mybutton. setwidth (INT) btn_w); mybutton. setheight (INT) btn_h); 4. With layout resources, android can define the layout of components on the screen in an XML file and call activity. the setcontentview () method displays the layout file on the activity. Android parses the components in the XML file into visualized view component layout. XML code using the layoutinflater class. <? Xmlversion = "1.0" encoding = "UTF-8"?> <Linearlayoutxmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <! -- The above four attributes are namespace, layout width, height (full view), component layout (vertical) --> <! -- The following nested tablelayout --> <tablelayout Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: stretchcolumns = "1"> <tablerow> <textview Android: id = "@ + ID/layouttextview01" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "@ string/test_view01" Android: background = "@ color/bk_col"/> <! -- The above attributes: reference component ID, text content, width of the component, height of the component, text content, background color --> <edittext Android: id = "@ + ID/edittext01" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = ""/> </tablerow> <tablerowandroid: gravity = "right"> <button Android: Id = "@ + ID/layoutbutton01" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "@ string/button_string"/> </tablerow> </tablelayout> </linearlayout> test the public class mainactivity extends activity {
// Define the component
Private button mybutton;
Private edittext myedittext;
Private textview mytextview;

@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Set the activity interface Layout
Setcontentview (R. layout. layout );
// Display components
Mytextview = (textview) findviewbyid (R. Id. layouttextview01 );
Mybutton = (button) findviewbyid (R. Id. layoutbutton01 );
Myedittext = (edittext) findviewbyid (R. Id. edittext01 );

} 5 Use menu resources Android menus can be divided into option menus, context menus and sub-menu structures: <menu> root element, the <item> and <group> sub-elements are nested in the <menu> root element. <menu> can also be nested in the <item> element to form a sub-menu. <group> indicates a menu group, attribute description: ID: uniquely identifies the reference idmenucategory of the menu group. It classifies menus and defines the limited menu level. Valid values include container, system, secondary, and alternativeorderincategory: one sorting integer checkablebehavior: Select behavior, single choice, multiple choice or other, valid value is none, all, singlevisible: visible, true or falseenabled: available, true or false <item> indicates a menu item, including valid attributes in <menu> or <group>. attribute description of <item> element: ID: the unique menu ID references menucategory: menu category orderincategory: Sorting by category title: menu title string titlecondensed: concentrated title. It is suitable for the use of icon: menu icon alphabticshortcut when the title is too long: character shortcut: numericshortcut: Number shortcut: checkable: Optional checked: Selected visible: visible enabled: available

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.