Remember that there was a gallery, but this later by Google gave up, the use of gallery people must know the bad use, now more is to use Viewpager or Horizontalscrollview, today say Horizontalscrollview the simple use of it, before the project has not been used to the control, thought it is the same as the ListView, only to find Horizontalscrollview there is no Setadapter () method, How do you bind some resources to a Horizontalscrollview control? In another way, we can add a linearlayout control inside, because linearlayout it has directional properties, now create a new Android project/ Horizontaldemo,
Activity_main.xml
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
>
Android:id= "@+id/hsv"
Android:layout_width= "Fill_parent"
android:layout_height= "180DP"
android:layout_margintop= "20DP"
>
<linearlayout
Android:id= "@+id/ll"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:orientation= "Horizontal"
></LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
Mainactivity.java
Package Com.example.horizontaldemo;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.HorizontalScrollView;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
Import Android.widget.TextView;
public class Mainactivity extends Activity {
Private Horizontalscrollview HSV;
Private LinearLayout ll;
Private int[] Mimgids;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
HSV = (Horizontalscrollview) Findviewbyid (R.ID.HSV);
ll = (linearlayout) Findviewbyid (R.ID.LL);
InitData ();
Initview ();
}
private void Initview () {
for (int i=0;i<mimgids.length;i++) {
View Itemview = Getlayoutinflater (). Inflate (R.layout.item, NULL);
ImageView IV = (ImageView) Itemview.findviewbyid (R.ID.IV);
TextView tv_name = (TextView) Itemview.findviewbyid (r.id.tv_name);
Iv.setimageresource (Mimgids[i]);
Tv_name.settext ("Sister One");
Ll.addview (Itemview);
}
}
private void InitData ()
{
Mimgids = new int[] {r.drawable.a, r.drawable.b, r.drawable.c,
R.DRAWABLE.D, R.DRAWABLE.E, R.drawable.f, r.drawable.g,
R.drawable.h, R.DRAWABLE.L};
}
}
Item.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >
<imageview
Android:id= "@+id/iv"
Android:layout_width= "90DP"
android:layout_height= "90DP"
/>
<textview
Android:id= "@+id/tv_name"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_gravity= "Center"
/>
</LinearLayout>
:
Thanks for the photo resources of http://blog.csdn.net/lmj623565791/article/details/38140505 Blog in this
Simple use of Android Horizontalscrollview