Simple use of android HorizontalScrollView, androidscroll

Source: Internet
Author: User

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.

Related Article

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.