A small browser resource effect for Android

Source: Internet
Author: User

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:

 
 
  1. System.out.print("Hello ---------------------\n");   
  2. Log.d("WEI","Hi ------------------1-----------");   
  3. 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:

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <Gallery xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:id="@+id/gallery" 
  4. android:layout_width="fill_parent" 
  5. android:layout_height="wrap_content"> 
  6. </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:

 
 
  1. Public class Chapter7Test8 extends Activity {
  2. @ Override
  3. Protected void onCreate (Bundle savedInstanceState ){
  4. Super. onCreate (savedInstanceState );
  5. SetContentView (R. layout. chapter_7_test8 );
  6. // 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.
  7. Gallery gallery = (Gallery) findViewById (R. id. gallery );
  8. Gallery. setAdapter (new ImageAdapter (this ));
  9. Gallery. setOnItemClickListener (new OnItemClickListener (){
  10. Public void onItemClick (AdapterView <?> Parent, View v, int position, long id ){
  11. Toast. makeText (Chapter7Test8. this, "" + position, Toast. LENGTH_SHORT). show ();
  12. }
  13. });
  14. }
  15.  
  16. // Step 2: The adapter inherits the BaseAdapter and describes the item. You must create a constructor to specify getCount (), getItem (), getItemId (), and getView ().
  17. Private class ImageAdapter extends BaseAdapter {
  18. Private Context mContext;
  19. Private Integer [] mImageIds = {R. drawable. sample_1, R. drawable. sample_2, R. drawable. sample_3,
  20. R. drawable. sample_4, R. drawable. sample_5, R. drawable. sample_6,
  21. R. drawable. sample_7}; // copy the image file named drawable_sample_1 to drawable.
  22. Public ImageAdapter (Context context ){
  23. MContext = context;
  24. }
  25. Public int getCount (){
  26. Return mImageIds. length;
  27. }
  28. Public Object getItem (int position ){
  29. Return position;
  30. }
  31. Public long getItemId (int position ){
  32. Return position;
  33. }
  34. // 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.
  35. Public View getView (int position, View convertView, ViewGroup parent ){
  36. ImageView image = new ImageView (mContext );
  37. Image. setImageResource (mImageIds [position]);
  38. Image. setLayoutParams (new Gallery. LayoutParams (150,100 ));
  39. Image. setScaleType (ImageView. ScaleType. FIT_XY );
  40. Return image;
  41. }
  42. }
  43. }
  44. 3) set the item format through the xml file
  45. Add an xml file under res/values/to describe the property format of the custom widget.
  46. Java code:
  47. <Resources>
  48. <Declare-styleable name = "XXXX">
  49. <Attr name = "AAAAA" format = "BBBB"/>
  50. <Attr name = "aaaaa" format = "bbbb"/>
  51. </Declare-styleable>
  52. </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:

 
 
  1. <resources> 
  2. <declare-styleable name="HelloGallery"> 
  3. <attr name="android:galleryItemBackground" /> 
  4. </declare-styleable> 
  5. </resources> 

How to Get custom attributes:

Java code:

 
 
  1. TypedArray a = obtainStyledAttributes (R. styleable. XXX/* int [] */);
  2. 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.
  3. A. recyle (), // it should be called after obtainStyledAttributes () is used. Yes, it can be reused by the system.

In this example:

Java code:

 
 
  1. public ImageAdapter(Context context){  
  2. TypedArray a= obtainStyledAttributes(R.styleable.HelloGallery);  
  3. mGalleryItemBackground = a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground , 0);  
  4. a.recycle();  
  5. }  
  6. public View getView(int position, View convertView, ViewGroup parent) {  
  7. image.setBackgroundResource (mGalleryItemBackground);  
  8. }  

Introduction to Android development platform-based

Android development: Passing values between activities

Android development: Custom GridView/ListView Data Source

Related Article

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.