The effect is as follows:
A total of one activity and two XML.
When the component we need to use is not in the layout file of the Setcontentview (), then we need to use the inflate () method to get it. Call Findviewbyid () using the View object, and the author calls Findviewbyid directly. Go a lot of detours, so write this blog to help you Bo friends, know is very easy.
*******
Mainactivity.java
Package Com.example.head;import Android.os.bundle;import Android.app.activity;import android.app.AlertDialog; Import Android.content.context;import Android.content.dialoginterface;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.baseadapter;import Android.widget.Gallery; Import Android.widget.imagebutton;import Android.widget.imageswitcher;import Android.widget.imageview;import Android.widget.viewswitcher.viewfactory;public class Mainactivity extends Activity implements viewfactory{ ImageButton ImageButton; Gallery gallery;imageswitcher imageswitcher;int index;private final static integer[] IMAGES = {R.DRAWABLE.Z1, R.DRAWABLE.Z2,R.DRAWABLE.Z3}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); ImageButton = (ImageButton) Findviewbyid (R.ID.IB01); Imagebutton.setBackgroundresource (R.DRAWABLE.Z1);} public void Click (View v) {layoutinflater layoutinflater =layoutinflater.from (mainactivity.this); View view = Layoutinflater.inflate (r.layout.headchoose, NULL); Alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this); imageswitcher= (Imageswitcher) View.findviewbyid (R.ID.IS01); The component is not activity_main. So first use inflate to get the headchoose layout//and assign values to the View object. Use the View object to invoke the Findviewbyid () method. If you do not use the view to//call Findviewbyid (), can not find an error. Gallery = (gallery) View.findviewbyid (R.ID.GALL01); imageswitcher.setfactory (mainactivity.this); gallery.setadapter (New Imageadapter (Mainactivity.this)); Gallery.setonitemclicklistener (new Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> arg0, View arg1,int position, long Arg3) {Imageswitcher.setimageresource (images[ Position]); index = position;}}); Builder.settitle ("Picture Selection"). Setview (view). Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialog, int which) {IMAGEBUTTON.SETBACkgroundresource (Images[index]);}); Builder.show ();} @Overridepublic View Makeview () {//TODO auto-generated method Stubreturn new ImageView (this);} /* * Imageadapter * */class Imageadapter extends Baseadapter{context context;public imageadapter (context context) {THIS.C Ontext = context;} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn images.length;} @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn position;} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn position;} @Overridepublic view GetView (int position, view V, ViewGroup VG) {ImageView ImageView = new ImageView (context); IMAGEVIEW.S Etimageresource (Images[position]); Imageview.setlayoutparams (new Gallery.layoutparams (120, 120)); Imageview.setscaletype (ImageView.ScaleType.FIT_CENTER); return ImageView;}}}
Activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent " android:orientation=" Horizontal "> <imagebutton android:id= "@+id/ib01" android:layout_width= "70DP" android:layout_height= "70DP" android:o nclick= "click"/> <edittext android:layout_width= "fill_parent" android:layout_height= "Wrap_ Content "/></linearlayout>
Headchoose.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent "> <gallery android:id= "@+id/gall01" android:layout_width= "fill_parent" android:spacing= "10DP" android: layout_height= "90DP"/> <imageswitcher android:id= "@+id/is01" android:layout_width= "90DP" android:layout_height= "90DP" android:layout_centerhorizontal= "true" > </ImageSwitcher> </RelativeLayout>
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
Learn about the Android Mini-selection program seventh (they define dialog boxes, Gallery, Imageswitcher)