In this chapter, we will introduce the Gallery controls and ImageSwitcher controls. This time, monsters said that the iphone is very popular now. The last communication commands were difficult to see and there were no interactions. Is there any nice-looking communication commands? Well, this time we will combine the two controls to make a good-looking general order.
OK. Let's start with main. xml.
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<TextView android: text = "Travel Notes to the West" android: id = "@ + id/textView1"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </TextView>
<ImageSwitcher android: id = "@ + id/imageSwitcher"
Android: layout_width = "320dp" android: layout_height = "280dp"> </ImageSwitcher>
<Gallery android: layout_width = "match_parent"
Android: layout_height = "wrap_content" android: id = "@ + id/gallery1"> </Gallery>
<TextView android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: text = "@ string/hello"
Android: id = "@ + id/text"> </TextView>
</LinearLayout>
The main Layout defines an ImageSwitcher and a Gallery control.
Then, a Layout of gallery_item.xml is defined, which is a Layout with both graphs and words in the Gallery control.
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<ImageView android: id = "@ + id/item_imageView" android: src = "@ drawable/bajie"
Android: layout_gravity = "center" android: scaleType = "fitXY"
Android: layout_marginLeft = "30dp" android: layout_marginRight = "30dp"
Android: layout_height = "wrap_content" android: layout_width = "wrap_content"> </ImageView>
<TextView android: text = "TextView" android: id = "@ + id/item_textView"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"
Android: layout_gravity = "center" android: layout_marginLeft = "30dp"
Android: layout_marginRight = "30dp"> </TextView>
</LinearLayout>
Well, now the main java code: www.2cto.com
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup;
Import android. view. ViewGroup. LayoutParams;
Import android. widget. AdapterView;
Import android. widget. AdapterView. OnItemSelectedListener;
Import android. widget. BaseAdapter;
Import android. widget. Gallery;
Import android. widget. ImageSwitcher;
Import android. widget. ImageView;
Import android. widget. TextView;
Import android. widget. ViewSwitcher. ViewFactory;
Public class ButtonDemoActivity extends Activity
{
Private TextView text = null;
Private int [] image = {R. drawable. tangseng, R. drawable. wukong,
R. drawable. bajie, R. drawable. shaseng };
Private String [] item = {"Tang Seng", "Sun Wukong", "", "Sha Monk "};
Private Gallery gallery;
Private ImageSwitcher switcher;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Find the TextView control in main. xml by ID
Text = (TextView) findViewById (R. id. text );
// Find the ImageSwitcher control in main. xml by ID
Switcher = (ImageSwitcher) findViewById (R. id. imageSwitcher );
Switcher. setFactory (new ViewFactory ()
{
// Create a view in ImageSwitcher
@ Override
Public View makeView ()
{
ImageView imageView = new ImageView (ButtonDemoActivity. this );
ImageView. setScaleType (ImageView. ScaleType. FIT_XY );
ImageView. setPadding (10, 10, 10, 10 );
ImageView. setLayoutParams (new ImageSwitcher. LayoutParams (
LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT ));
Return imageView;
}
});
// Find the Gallery control in main. xml by ID
Gallery = (Gallery) findViewById (R. id. gallery1 );
// Set the Gallery Adapter
BaseAdapter adapter = new BaseAdapter ()
{
// Obtain the view in the adapter
@ Override
Public View getView (int position, View convertView, ViewGroup parent)
{
// Return the view control buffered by the adapter
View view = convertView;
If (view = null)
{
// Adapt gallery_item.xml to View
LayoutInflater inflater = LayoutInflater
. From (getApplicationContext ());
View = inflater. inflate (R. layout. gallery_item, null );
}
// Find the ImageView control in gallery_item.xml
ImageView imageView = (ImageView) view
. FindViewById (R. id. item_imageView );
ImageView. setImageResource (image [position]);
// Search for the TextView control in gallery_item.xml
TextView textView = (TextView) view
. FindViewById (R. id. item_textView );
TextView. setText (item [position]);
Return view;
}
@ Override
Public long getItemId (int position)
{
Return position;
}
@ Override
Public Object getItem (int position)
{
Return position;
}
@ Override
Public int getCount ()
{
Return image. length;
}
};
Gallery. setAdapter (adapter );
// Set the listener for single Gallery Selection
Gallery. setOnItemSelectedListener (new OnItemSelectedListener ()
{
@ Override
Public void onItemSelected (AdapterView <?> Arg0, View arg1,
Int position, long arg3)
{
String str = "what you are viewing now is" + item [position];
UpdateText (str );
Switcher. setImageResource (image [position]);
}
@ Override
Public void onNothingSelected (AdapterView <?> Arg0)
{
// TODO Auto-generated method stub
}
});
}
Private void updateText (String string)
{
// Set the text information to display in the TextView control.
Text. setText (string );
}
}
Oh, the suggestions of the monsters are satisfied. The monsters are happy to catch Tang Miao. Well, this chapter is over. Thank you.
From: kangkangz4 Column