当前位置: 移动技术网 > 移动技术>移动开发>Android > Android HorizontalScrollView左右滑动效果

Android HorizontalScrollView左右滑动效果

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例为大家分享了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);
  }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网