Android HorizontalScrollView左右滑動(dòng)效果
本文實(shí)例為大家分享了Android HorizontalScrollView左右滑動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
效果圖

一.什么是HorizontalScrollView
HorizontalScrollView實(shí)際上是一個(gè)FrameLayout ,這意味著你只能在它下面放置一個(gè)子控件 ,這個(gè)子控件可以包含很多數(shù)據(jù)內(nèi)容。有可能這個(gè)子控件本身就是一個(gè)布局控件,可以包含非常多的其他用來展示數(shù)據(jù)的控件。這個(gè)布局控件一般使用的是一個(gè)水平布局的LinearLayout。TextView也是一個(gè)可滾動(dòng)的視圖控件,所以一般不需要HorizontalScrollView一般通過放置一個(gè)LinearLayout子控件。如果要使其添加其他的控件,就使用LinearLayout子控件來添加其他的控件,最后達(dá)到豐富其內(nèi)容的效果。
二.使用HorizontalScrollView實(shí)現(xiàn)左右滑動(dòng)的效果
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.新建一個(gè)布局文件item_text.xml并添加一個(gè)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.創(chuàng)建數(shù)據(jù)集,然后實(shí)例化子控件LinearLayout
4.創(chuàng)建一個(gè)int數(shù)組并把圖片放到數(shù)組中
5.聲明一個(gè)inintent方法
6.使用For循環(huán)開始添加數(shù)據(jù)
7.尋找行布局,第一個(gè)參數(shù)為行布局ID,第二個(gè)參數(shù)為這個(gè)行布局需要放到那個(gè)容器上
8.通過View尋找ID實(shí)例化控件
9.將int數(shù)組中的數(shù)據(jù)放到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);
//開始添加數(shù)據(jù)
for(int x=0; x<image.length; x++){
//尋找行布局,第一個(gè)參數(shù)為行布局ID,第二個(gè)參數(shù)為這個(gè)行布局需要放到那個(gè)容器上
View view=LayoutInflater.from(this).inflate(R.layout.item_text,mLinearLayout,false);
//通過View尋找ID實(shí)例化控件
ImageView img= (ImageView) view.findViewById(R.id.imageView);
//實(shí)例化TextView控件
TextView tv= (TextView) view.findViewById(R.id.textView);
//將int數(shù)組中的數(shù)據(jù)放到ImageView中
img.setImageResource(image[x]);
//給TextView添加文字
tv.setText("第"+(x+1)+"張");
//把行布局放到linear里
mLinearLayout.addView(view);
}
}
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Service(不和用戶交互應(yīng)用組件)案例分析
Service是在一段不定的時(shí)間運(yùn)行在后臺(tái),不和用戶交互應(yīng)用組件,本文將詳細(xì)介紹,需要了解的朋友可以參考下2012-12-12
詳解有關(guān)Android截圖與錄屏功能的學(xué)習(xí)
這篇文章主要介紹了詳解有關(guān)Android截圖與錄屏功能的學(xué)習(xí),詳細(xì)介紹如何使用MediaProjection,MediaCodec以及MediaMuxer來實(shí)現(xiàn)簡(jiǎn)單的截屏和錄屏功能。2017-04-04
Android startActivityForResult()代替方案示例
這篇文章主要為大家介紹了Android startActivityForResult()代替方案示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Android編程實(shí)現(xiàn)Gallery中每次滑動(dòng)只顯示一頁(yè)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)Gallery中每次滑動(dòng)只顯示一頁(yè)的方法,涉及Android擴(kuò)展Gallery控件實(shí)現(xiàn)翻頁(yè)效果控制的功能,涉及Android事件響應(yīng)及屬性控制的相關(guān)技巧,需要的朋友可以參考下2015-11-11
android studio節(jié)省C盤空間的配置方法
這篇文章主要介紹了android studio節(jié)省C盤空間的配置方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-07-07
Android自定義星星可滑動(dòng)評(píng)分控件
這篇文章主要介紹了Android自定義星星可滑動(dòng)評(píng)分控件,通過線性布局結(jié)合ImageView實(shí)現(xiàn)評(píng)分控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
android實(shí)現(xiàn)雙日期選擇控件(可隱藏日,只顯示年月)
本篇文章主要介紹了android實(shí)現(xiàn)雙日期選擇控件(可隱藏日,只顯示年月) ,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-01-01
OpenGL Shader實(shí)例分析(1)Wave效果
這篇文章主要為大家詳細(xì)介紹了OpenGL Shader實(shí)例分析第一篇,Wave效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
Android Toast使用的簡(jiǎn)單小結(jié)(推薦)
這篇文章主要介紹了Android Toast使用的簡(jiǎn)單小結(jié),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03

