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:
GalleyView
Galley is a gallery. It is generally used only for Image Display and is not commonly used.
1) Android XML file
Java code:
- <?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, see Android study note 13): Activity-GridView to inherit BaseAdapter.
2) Java source code
Java 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 (new ImageAdapter (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.
- Java code:
- <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:
Java code:
- <resources>
- <declare-styleable name="HelloGallery">
- <attr name="android:galleryItemBackground" />
- </declare-styleable>
- </resources>
How to Get custom attributes:
Java code:
- TypedArray a = obtainStyledAttributes (R. styleable. XXX/* int [] */);
- AattrId = a. getResourceId (R. styleable. XXXX_AAAA, defaultId); // obtain 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:
Java code:
- 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);
- }
Introduction to Android development platform-based
Android development: Passing values between activities
Android development: Custom GridView/ListView Data Source