Create a View. The list is as follows:
View_gallery.xml
[Html]
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
</LinearLayout>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
</LinearLayout>
Drag the icon on the panel and change the Properties as follows:
[Html]
<Gallery
Android: id = "@ + id/gallery02"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "30dp"/>
<Gallery
Android: id = "@ + id/gallery02"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "30dp"/>
Add a button as follows:
[Html] v
<Button
Android: id = "@ + id/btnReturn"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "166dp"
Android: text = "@ string/btn1Caption"/>
<Button
Android: id = "@ + id/btnReturn"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "166dp"
Android: text = "@ string/btn1Caption"/>
The overall layout is as follows:
Change the "Push me" button to the switch button. That is, the switchover function is implemented. For more information about the code, see switch multiple views!
To implement Gallery, you must inherit the BaseAdapter. we name it ImgAdapter.
The code list is as follows:
[Java]
Package com. example. prjandroid;
Import android. content. Context;
Import android. content. res. TypedArray;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. BaseAdapter;
Import android. widget. Gallery;
Import android. widget. ImageView;
Public class ImgAdapter extends BaseAdapter {
@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return null;
}
@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return position;
}
@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
// TODO Auto-generated method stub
Return null;
}
}
Package com. example. prjandroid;
Import android. content. Context;
Import android. content. res. TypedArray;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. BaseAdapter;
Import android. widget. Gallery;
Import android. widget. ImageView;
Public class ImgAdapter extends BaseAdapter {
@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return null;
}
@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return position;
}
@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
// TODO Auto-generated method stub
Return null;
}
}
To put an image, you must use ImageView!
Put the image under res/drawable (png format is recommended ).
Then, create an xml file to provide the background of Gallery. As follows:
Res/attrs. xml
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Declare-styleable name = "galleryThem">
<Attr name = "android: galleryItemBackground"/>
</Declare-styleable>
</Resources>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Declare-styleable name = "galleryThem">
<Attr name = "android: galleryItemBackground"/>
</Declare-styleable>
</Resources>
Declare an array of image IDs:
[Java]
// Resource draw
Private int [] resPics = new int [] {
R. drawable. emacs1,
R. drawable. emacs2,
R. drawable. emacs3,
R. drawable. emacs4,
R. drawable. emacs5,
R. drawable. emacs6,
R. drawable. emacs7,
R. drawable. emacs8,
R. drawable. emacs9,
R. drawable. emacs10
};
// Resource draw
Private int [] resPics = new int [] {
R. drawable. emacs1,
R. drawable. emacs2,
R. drawable. emacs3,
R. drawable. emacs4,
R. drawable. emacs5,
R. drawable. emacs6,
R. drawable. emacs7,
R. drawable. emacs8,
R. drawable. emacs9,
R. drawable. emacs10
};
Use ImageView in the getView () method:
[Java]
ImageView imgView = new ImageView (m_context );
ImgView. setImageResource (resPics [position]);
ImgView. setScaleType (ImageView. ScaleType. FIT_XY );
ImgView. setLayoutParams (new Gallery. LayoutParams (163,106 ));
ImgView. setBackgroundResource (m_galleryItemBackGround );
Return imgView;
ImageView imgView = new ImageView (m_context );
ImgView. setImageResource (resPics [position]);
ImgView. setScaleType (ImageView. ScaleType. FIT_XY );
ImgView. setLayoutParams (new Gallery. LayoutParams (163,106 ));
ImgView. setBackgroundResource (m_galleryItemBackGround );
Return imgView;
There are eight types of ImageView. ScaleType:
1. ImageView. ScaleType. center: The image is located in the middle of the view without scaling.
2. ImageView. ScaleType. CENTER_CROP scales the image in a unified proportion (maintaining the size ratio of the image) so that the two dimensions (width and height) of the image are equal to or greater than the corresponding view dimension.
3. ImageView. ScaleType. CENTER_INSIDE scales the image in a unified proportion (maintaining the size ratio of the image) so that the two dimensions (width and height) of the image are equal to or less than the corresponding view dimension.
4. ImageView. ScaleType. FIT_CENTER: Use center to scale the image
5. ImageView. ScaleType. FIT_END: Use END to scale the image.
6. Use START to scale the image using ImageView. ScaleType. FIT_START
7. ImageView. ScaleType. FIT_XY: Use XY to scale the image
8. ImageView. ScaleType. MATRIX use image MATRIX scaling when drawing
In addition, Context is equivalent to Handle in windows.
When constructing this class, we need Context and initialize the Gallery attributes. The related code is as follows:
[Java]
Public ImgAdapter (Context context ){
// TODO Auto-generated constructor stub
M_context = context;
TypedArray typeArray =
M_context.obtainStyledAttributes (R. styleable. galleryThem );
M_galleryItemBackGround = typeArray. getResourceId (
R. styleable. galleryThem_android_galleryItemBackground, 0 );
}
Public ImgAdapter (Context context ){
// TODO Auto-generated constructor stub
M_context = context;
TypedArray typeArray =
M_context.obtainStyledAttributes (R. styleable. galleryThem );
M_galleryItemBackGround = typeArray. getResourceId (
R. styleable. galleryThem_android_galleryItemBackground, 0 );
}
Basically, this adaptation class looks like this.
The calling code is similar to the previous example of a Spinner! The difference is that this is directly transmitted by the Spinner, but must be passed to the main class. this (for example, MainActivity. this ).
The running effect is as follows: