Java-android Horizontalscrollview linearlayout Pictures view

Source: Internet
Author: User

The main production of image browsing features, as follows:

Principle: Horizontalscrollview This control, itself can move horizontally, now say can move the Horizontalscrollview, add a linearlayout layout file, you can achieve this effect.

Therefore, in the LinearLayout processing 2 questions, one is the data correspondence problem, two, the layout question.

Difficulty: The use of Layoutinflater

It is still very useful to layoutinflater this class in real-world development, which acts like Findviewbyid (). The difference is that Layoutinflater is used to find the XML layout file under res/layout/and is instantiated, while Findviewbyid () is looking for specific widget controls (such as button, TextView, and so on) under the XML layout file. Specific role: 1, for a non-loaded or want to dynamically load the interface, you need to use Layoutinflater.inflate () to load, 2, for an already loaded interface, you can use Activiyt.findviewbyid () method to obtain the interface elements. Layoutinflater is an abstract class that is declared in the document as follows: Public abstract class Layoutinflater extends object obtains three ways of Layoutinflater instance 1. Layoutinflater inflater = Getlayoutinflater ();//Invoke Activity's Getlayoutinflater ()  2. Layoutinflater Inflater = layoutinflater.from (context);  3. Layoutinflater inflater =   (layoutinflater) context.getsystemservice                              (context.layout_inflater_service); in fact, the three kinds of ways are essentially the same, From the source can be seen: Getlayoutinflater (): Activity of the Getlayoutinflater () method is to call Phonewindow Getlayoutinflater () method, look at the source code: public Phonewindow (Context context) {   super (context);       Mlayoutinflater = Layoutinflater. from (context);} It can be seen that it is actually called layoutinflater.from (context). Layoutinflater.from (context): public static Layoutinflater from (context context) {   layoutinflater Layoutinflater = (layoutinflater) context.getsystemservice          (context.layout_inflater_ SERVICE);    if (layoutinflater = = null)     {           throw New Assertionerror ("Layoutinflater not Found.");      }       return layoutinflater;} It can be seen that it actually calls Context.getsystemservice (). Conclusion: So the final essence of these three methods is all called Context.getsystemservice (). Another Getsystemservice () is an important API for Android, a method of activity that obtains the corresponding object according to the name passed in, and then translates it into the corresponding service object. The following describes the system's corresponding services.   Incoming name returns the object description Window_service WindowManager Manage open Windows program Layout_inflater_service layoutinflater Get the system state of the Viewactivity_service Activitymanager management application defined in XML Power_service Powermanger power supply Alarm_service Alarmmanager Alarm Clock service notification_service notificationmanager status bar service Keyguard_service KEyguardmanager Keypad Lock Service Location_service Locationmanager location services, such as Gpssearch_service Searchmanager Search service Vebrator_service Vebrator Phone Shake service connectivity_service Connectivity network connected service Wifi_service Wifimanager Wi-Fi service Telephony_service Teleponymanager Telephone Service inflate method through the API documentation of the SDK, you can know that the method has the following overload forms, the return value is the View object, as follows: public View inflate (int resource, Viewgro Up root)  public view inflate (Xmlpullparser parser, ViewGroup root) public View inflate (xmlpullparser parser, Viewgro Up root, Boolean attachtoroot)  public View Inflate (int resource, ViewGroup root, Boolean attachtoroot)

Layout file xml

1<?xml version="1.0"encoding="Utf-8"?>2<absolutelayout xmlns:android="http://schemas.android.com/apk/res/android"3Android:layout_width="match_parent"4android:layout_height="match_parent">5 6<Horizontalscrollview7Android:id="@+id/horizontalscrollview1"8Android:layout_width="256DP"9android:layout_height="364DP"Tenandroid:layout_x="28DP" Oneandroid:layout_y="40DP"> A  -<LinearLayout -Android:id="@+id/gallerylinearlayout" theAndroid:layout_width="match_parent" -android:layout_height="match_parent" -android:orientation="Horizontal"> -  +</LinearLayout> -</HorizontalScrollView> +  A</AbsoluteLayout>

Show file xml

1<?xml version="1.0"encoding="Utf-8"?>2<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"3Android:layout_width="wrap_content"4android:layout_height="wrap_content">5 6<ImageView7Android:id="@+id/imageview"8Android:layout_width="80dip"9android:layout_height="80dip"TenAndroid:layout_centerhorizontal="true"/> One  A<TextView -Android:id="@+id/textview" -Android:layout_width="wrap_content" theandroid:layout_height="wrap_content" -android:layout_below="@id/imageview" -Android:layout_centerhorizontal="true" -android:layout_margintop="8dip"/> +  -</RelativeLayout>

Source Code Java

1 Package com.test;2 3 import Android.os.Bundle;4 import android.app.Activity;5 import Android.view.LayoutInflater;6 import Android.view.Menu;7 import Android.view.View;8 import android.widget.Gallery;9 import Android.widget.ImageView;Ten import android.widget.LinearLayout; One import Android.widget.TextView; A  -  Public classMainactivity extends Activity { -      the     Private int[] mphotosintarray; -     PrivateLayoutinflater Mlayoutinflater; -     Privatelinearlayout mgallerylinearlayout; -  + @Override -     protected voidonCreate (Bundle savedinstancestate) { + super.oncreate (savedinstancestate); A Setcontentview (r.layout.tt); atMgallerylinearlayout= (LinearLayout) This. Findviewbyid (r.id.gallerylinearlayout); -Mlayoutinflater=layoutinflater. from( This); -mphotosintarray=New int[]{r.drawable.a1,r.drawable.a2,r.drawable.a3,r.drawable.a4,r.drawable.a5,r.drawable.a6, - r.drawable.a7,r.drawable.a8,r.drawable.a9}; -          -View itemview=NULL; inImageView imageview=NULL; - TextView TextView; to          for(inti =0; i < mphotosintarray.length; i++) { +Itemview=mlayoutinflater.inflate (R.LAYOUT.TTT,NULL); -imageview=(ImageView) Itemview.findviewbyid (R.id.imageview); thetextview=(TextView) Itemview.findviewbyid (R.id.textview); * Imageview.setimageresource (Mphotosintarray[i]); $ Imageview.setscaletype (Imageview.scaletype.  Center_crop); Panax NotoginsengTextview.settext (" This is", (n+1)); - Mgallerylinearlayout.addview (itemview); the         } +  A          the          +          -     } $  $  -      -}

Java-android Horizontalscrollview linearlayout Pictures view

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.