
本文实例为大家分享了AndroID horizontalscrollview左右滑动的具体代码,供大家参考,具体内容如下
效果图
一.什么是horizontalscrollview
horizontalscrollview实际上是一个FrameLayout,这意味着你只能在它下面放置一个子控件 ,这个子控件可以包含很多数据内容。有可能这个子控件本身就是一个布局控件,可以包含非常多的其他用来展示数据的控件。这个布局控件一般使用的是一个水平布局的linearLayout。TextVIEw也是一个可滚动的视图控件,所以一般不需要horizontalscrollview一般通过放置一个linearLayout子控件。如果要使其添加其他的控件,就使用linearLayout子控件来添加其他的控件,最后达到丰富其内容的效果。
二.使用horizontalscrollview实现左右滑动的效果
1.编写布局文件activity_main.xml
<?xml version="1.0" enCoding="utf-8"?><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" tools:context="com.example.cxy.horizontalscrollview.MainActivity"> <horizontalscrollview androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/horizontalscrollview" androID:layout_alignParenttop="true" androID:layout_centerHorizontal="true"> <linearLayout androID:ID="@+ID/linear" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal"> </linearLayout> </horizontalscrollview></relativeLayout>
2.新建一个布局文件item_text.xml并添加一个ImageVIEw和TextVIEw
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:paddingRight="2dp" androID:paddingleft="2dp"> <ImageVIEw androID:layout_wIDth="100dp" androID:layout_height="100dp" androID:ID="@+ID/imageVIEw" androID:layout_gravity="center_horizontal" androID:layout_alignParenttop="true" androID:layout_alignleft="@+ID/textVIEw" androID:layout_alignStart="@+ID/textVIEw"/> <TextVIEw androID:textSize="30dp" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="New Text" androID:ID="@+ID/textVIEw" androID:layout_below="@+ID/imageVIEw" androID:layout_centerHorizontal="true"/></relativeLayout>
3.创建数据集,然后实例化子控件linearLayout
4.创建一个int数组并把图片放到数组中
5.声明一个inintent方法
6.使用For循环开始添加数据
7.寻找行布局,第一个参数为行布局ID,第二个参数为这个行布局需要放到那个容器上
8.通过VIEw寻找ID实例化控件
9.将int数组中的数据放到ImageVIEw中
10.给TextVIEw添加文字
11.把行布局放到linear里
package com.example.cxy.horizontalscrollview;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.Widget.ImageVIEw;import androID.Widget.linearLayout;import androID.Widget.TextVIEw;public class MainActivity extends AppCompatActivity { private linearLayout mlinearLayout; private int[] image={R.drawable.a11,R.drawable.a22,R.drawable.a33,R.drawable.a44,R.drawable.a55,R.drawable.a66,R.drawable.a77,R.drawable.a88,R.drawable.a99}; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); inintent(); } private voID inintent() { mlinearLayout= (linearLayout) findVIEwByID(R.ID.linear); //开始添加数据 for(int x=0; x<image.length; x++){ //寻找行布局,第一个参数为行布局ID,第二个参数为这个行布局需要放到那个容器上 VIEw vIEw=LayoutInflater.from(this).inflate(R.layout.item_text,mlinearLayout,false); //通过VIEw寻找ID实例化控件 ImageVIEw img= (ImageVIEw) vIEw.findVIEwByID(R.ID.imageVIEw); //实例化TextVIEw控件 TextVIEw tv= (TextVIEw) vIEw.findVIEwByID(R.ID.textVIEw); //将int数组中的数据放到ImageVIEw中 img.setimageResource(image[x]); //给TextVIEw添加文字 tv.setText("第"+(x+1)+"张"); //把行布局放到linear里 mlinearLayout.addVIEw(vIEw); } }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是内存溢出为你收集整理的Android HorizontalScrollView左右滑动效果全部内容,希望文章能够帮你解决Android HorizontalScrollView左右滑动效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)