Logcat commissioning Information
In Window-> Show View-> Other... -> Android-> LogCat, The LogCat window is displayed. out. print () and Log. d () can print the information we need, for example:
System. out. print ("Hello -------------------/n ");
Log. d ("WEI", "Hi ------------------ 1 -----------");
Log. d ("WEI", "Hi ------------------- 2 ----------");
In this way, we can see the relevant information in the LogCat query window. There are five levels of Log, namely v, d, I, w, e, which are called in a similar way using Log. w.
Galleyview
Galley is a gallery. It is generally used only for Image Display and is not commonly used.
1) Android XML file
<? Xml version = "1.0" encoding = "UTF-8"?>
<Gallery xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/gallery"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
</Gallery>
Because Galley users process images, ImageView can be used to process items. In setting the adapter, we can refer to Android learning note (13): Activity-GridView to inherit BaseAdapter.
2) Java source code
Public class Chapter7Test8 extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. chapter_7_test8 );
// Step 1: Set the adapter to describe the content of the item and set the format of the item, and set the operation triggered by clicking through setOnItemClickListener.
Gallery gallery = (Gallery) findViewById (R. id. gallery );
Gallery.Setadapter(NewImageadapter(This ));
Gallery.Setonitemclicklistener(New OnItemClickListener (){
Public void onItemClick (AdapterView <?> Parent, View v, int position, long id ){
Toast. makeText (Chapter7Test8. this, "" + position, Toast. LENGTH_SHORT). show ();
}
});
}
// Step 2: The adapter inherits the BaseAdapter and describes the item. You must create a constructor to specify getCount (), getItem (), getItemId (), and getView ().
Private class ImageAdapter extends BaseAdapter {
Private Context mContext;
Private Integer [] mImageIds = {R. drawable. sample_1, R. drawable. sample_2, R. drawable. sample_3,
R. drawable. sample_4, R. drawable. sample_5, R. drawable. sample_6,
R. drawable. sample_7}; // copy the image file named drawable_sample_1 to drawable.
Public ImageAdapter (Context context ){
MContext = context;
}
Public int getCount (){
Return mImageIds. length;
}
Public object getitem (INT position ){
Return position;
}
Public long getitemid (INT position ){
Return position;
}
// Step 3: each item is an imageview. You can use setimageresource to present the image, set the size of each item, and display ratio. Here, fit_xy is used, according to X: Y shows the entire image. If X: Y and the image length are different from width, the image may be distorted.
Public View getview (INT position, view convertview, viewgroup parent ){
Imageview image = new imageview (mcontext );
Image. setImageResource (mImageIds [position]);
Image. setLayoutParams (new Gallery. LayoutParams (150,100 ));
Image. setScaleType (ImageView. ScaleType. FIT_XY );
Return image;
}
}
}
3) set the item format through the xml file
Add an xml file under res/values/to describe the property format of the custom widget.
<Resources>
<Declare-styleable name = "XXXX">
<Attr name = "AAAAA"Format = "BBBB"/>
<Attr name = "aaaaa"Format = "BBBB"/>
</Declare-styleable>
</Resources>
Int R. styleable. XXXX [] is added in R. java to indicate this definition. If there are two attributes, there are two elements. In this example, set the style attribute. We set a property galleryItembackground defined by android, which defines a gallery item with a border. As follows:
<Resources>
<Declare-styleable name = "HelloGallery">
<Attr name = "android: galleryItemBackground"/>
</Declare-styleable>
</Resources>
How to Get custom attributes:
TypedArray a =ObtainStyledAttributes(R. styleable. XXX/* int [] */);
AttrId =.GetResourceId(R. styleable. XXXX_AAAA, defaultId); // gets the ID of this attribute. If this attribute is not found, the defaultId value is returned.
A.Recyle(), // It should be called after obtainStyledAttributes () is used. Yes, it can be reused by the system.
In this example:
Public ImageAdapter (Context context ){
......
TypedArray a = obtainStyledAttributes (R. styleable. HelloGallery );
Mgalleryitembackground = A. getresourceid (R. styleable. HelloGallery_android_galleryItemBackground, 0 );
A. Recycle ();
}
Public View getview (INT position, view convertview, viewgroup parent ){
......
Image.SetBackgroundResource(Mgalleryitembackground );
......
}
Related Links: My Andriod development articles