Simple use of android HorizontalScrollView, androidscroll
I remember that there was a Gallery in the past, but Google gave up this later. Those who used the Gallery certainly knew it was not easy to use. Now they are more likely to use ViewPager or HorizontalScrollView, today, let's talk about the simple use of HorizontalScrollView. This control has never been used in projects before. I thought it was used like listview, and then I found that HorizontalScrollView has no setAdapter () method, how can I bind some resources to the HorizontalScrollView control? In another way, we can add a LinearLayout control, because LinearLayout has a direction attribute. Now we create an 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"
>
<HorizontalScrollView
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 ("Mei Yi ");
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>
:
Thank you for your image resources at http://blog.csdn.net/lmj623565791/article/details/38140505blog.