Android--Resource usage and experience sharing

Source: Internet
Author: User

Color Resources

    • Color XML file format
<? XML version= "1.0" encoding= "Utf-8" ?>     < Resources >                 //resources root element         <name= "">#000000 </ Color >                                         //color child element      </resouces>
    • Reference format
// in Java code: R.color.color_name  //(this is an int type data that represents the address of the resource)
<!-- in the XML file:  - @color/color_name
    • Ways to get color values
Getresources (). GetColor ();   // returns a color value similar to #0000ff

string resource

    • XML file format
<? XML version= "1.0" encoding= "Utf-8" ?>     < Resources >                 //resources root element         <name= ""> Stringhere</string>                                        //string child elements      </ resouces >
    • Reference format
// in Java code:   R.string.string_name
<!-- - @string/string_name
    • Ways to get strings
resources.getstring (int);    // returns an object of type string // Note both the settext (int resid)   and SetText (String String_name)    methods are allowed in the control

Size Resources

    • XML file
<namedimen_value</dimen>      < !--  Size values can be in px (pixels)  or mm (mm, actual screen size) or DP--
    • Reference format
// Java code:   R.dimen.dimen_name
<!-- in the XML file    :  - @dimen/dimen_name
    • Get method
resources.getdimension (r.dimen.dimen_name); // Note that using this method returns a value of type float, whereas the setwidth () parameter is generally required to be of type int, so it is mandatory to cast the type one at a time. 

XML resource File

    • XML file

android程序自带的xml 文件一般放在values/xml/xml_name.xml  处,这里需要使用new->file床建一个新的xml文件;

xml文件的版本

<? XML version= "1.0" encoding= "Utf-8" ?>
    • Reference format
// Java R.xml.xml_name
    • How to get
Resources.getxml (int resid);   // a Xmlresourceparser object is returned.

Others experience summary sharing

    • Achieve rounded corners-Learn to use XML files in drawable/that describe backgrounds

You need to create an XML file in the Drawable folder, the file's parent control type is shape, and in the shape parent control, there are <solid/> <corners/> <stroke/> <padding/ > properties, respectively, handle the background fill color, corner curvature, border line width and color, upper and lower left and right inner border (that is, background beyond the width of the space used to change the background)

For example, if you want to implement a fillet ImageButton, you can create a fillet_shape.xml file

<?XML version= "1.0" encoding= "Utf-8"?><Shapexmlns:android= "Http://schemas.android.com/apk/res/android" >    <SolidAndroid:color= "#ffffff"/>    <CornersAndroid:radius= "10px"/>    <paddingAndroid:left= "3dip"Android:top= "3dip"Android:right= "3dip"Android:bottom= "3dip"/>    </Shape>

The Setbackgroundresource () is then set in the activity class with an instance of ImageButton; Or, in the XML layout file, use android:background= "@drawable/fillet_shape" in the configuration control properties. After you've configured the background, when you set the picture to display for ImageView, Only use Setimageresource () and cannot use Setbackgroundresource ();

    • How does the ListView adjust the width of each item border and avoid the split line color between item too deep?

method is the XML file in the custom drawable/described above to configure the edge and background properties, and set android:divider= "#aaaaaa" android:dividerheight= "0px when configuring the properties of the ListView control " 。

    • How do I troubleshoot a click event after adding a button in the ListView?

Because the button grabs the focus, the simplest solution is to add a android:descendantfocusability= "Blocksdescendants" to the properties of the root tag in each custom ListItem layout file: That is, the child controls in the ListItem are denied the focus.

    • How do I achieve the effect of making the control disappear when I click a screen area other than a control? In this example, the effect of clicking on an area other than the ListView will cause the ListView to disappear.

The method is to overwrite the mainactivity ontouchevent () method, according to the coordinates of the click (x, y) and the target control through GetLocation obtained by the upper-left coordinate of the control, combined with the width and height of the target control, determine whether the clicked point is within the control, It then determines what action to take on the control.

@Override Public Booleanontouchevent (Motionevent event) {//TODO auto-generated Method Stub        if(Event.getaction () ==motionevent.action_down &&isVisible) {            int[] location=New int[2]; //call the Getlocationinwindow method to get the horizontal ordinate of a control in the upper-left corner of the windowLoginlist.getlocationinwindow (location); //get the coordinates of the points clicked on the screen            intX= (int) Event.getx (); intY= (int) event.gety (); if(x<location[0]| | x>location[0]+loginlist.getwidth () | |y<location[1]| | y>location[1]+loginlist.getheight ()) {Isindicatorup=false; IsVisible=false;                Listindicatorbutton.setbackgroundresource (R.drawable.indicator_down);   Loginlist.setvisibility (View.gone); //Let the ListView list disappear and let the cursor point down!                             }                            }              return Super. Ontouchevent (event); }
    • How to use Layer-list

The role of layer-list is to stack multiple layers, as you can see from the name, because this section wants to make one side without a border, no border on the other edge of the background effect, using <shape > <stroke/> tags There is no way to meet, The solution is to create a layer-list file in the Drawable folder, then create two layers <item> The first item is the color of the border, the second layer is the inner color, and by setting the root Tag property of the second item, Sets the degree to which the second item is narrower than the first item so that the color of the first item is displayed at the border to make the border effect.

<?XML version= "1.0" encoding= "Utf-8"?><layer-listxmlns:android= "Http://schemas.android.com/apk/res/android" >    <Item>        <Shape>            <SolidAndroid:color= "#cccccc"/>        </Shape>   <!--above is the first layer -    </Item>                           <ItemAndroid:left= "1dip"Android:top= "1dip"Android:right= "1dip"Android:bottom= "0dip">         <!--The above line is the key step to set which border, the second layer is more than the first layer flash out of the border!  -        <Shape>            <SolidAndroid:color= "#f0f0f0"/>                   </Shape>    </Item></layer-list>
    • How to understand the life cycle of Expandablelistview?

For Expandablelistview, when the object has just been created and the adapter is set, each group of Elistview is not expanded, and when a group is clicked, the system will redraw all group and expanded ChildItem again. , where each of the group's last ChildItem put the islastchild to true.

The ongroupexpanded () and ongroupcollapsed () methods are called when a group is expanded or closed.

    • How to get the Expandablelistview height dynamically?

Creates an int variable height that, when Expandablelistview is just created, sets its height to the sum of all group widths, and then when the group is opened or closed, the ongroupexpanded () and the ongroupcollapsed () method increases or decreases the height value, and the Layoutparams object even updates the height of the space, enabling dynamic update Expandablelistview.

The specific code is as follows:

1, get the height of listitem, here such as Get group height, need to call the adapter's Getgroupview method

View listitem=  listview.getexpandableadapter (). Getgroupview (parametric); Listitem.setlayoutparams (New  Layoutparams (layoutparams.wrap_content,layoutparams.wrap_content)); // this must be because ListItem does not specify a parent control when it is obtained through the inflate () method, so the width and height cannot be resolved in the Onmeasure () method, so the layout type is coerced to the wrapcontent type. Then call the measure () method listitem.measure (0,0);      // 0,0 is a two parameter for comparison, the measure () method, which is used to calculate the size of the control, as detailed in the details of this method, refer to: http://blog.csdn.net/lilybaobei/article/details/8021868 int Height=listitem.getmeasuredheight ();

2, set the height of the Expandablelistview, through the Layoutparams object

Viewgroup.layoutparams params=listview.getlayoutparams (); // Obtain dimension parameter params.height=height*3; listview.setlayoutparams (params);

The above two points of the problem is mainly due to the ListView, Expandablelistview placed in a scrollview often appear incomplete problems, so the solution is to dynamically update the height of Listview/exlistview, To make the list appear intact.

    • The listview boundary is blurred and the color is dimmed

The solution to this problem is to add the android:fadingedge= "None" to the ListView's property settings so that the boundaries are not blurred.

I'm the dividing line of the king of the Land Tiger.

Reference: Http://www.cnblogs.com/carlos-vic

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.