Android HorizontalScrollView左右滑動效果
本文實例為大家分享了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.創(chuàng)建數據集,然后實例化子控件LinearLayout
4.創(chuàng)建一個int數組并把圖片放到數組中
5.聲明一個inintent方法
6.使用For循環(huán)開始添加數據
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 WorkManager實現后臺定時任務流程詳解
WorkManager是Android Jetpack的一個強大的組件,用于處理后臺耗時任務。后臺任務可以是一次性的,也可以是重復的,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-01-01Android開發(fā)之InetAddress基礎入門簡介與源碼實例
這篇文章主要介紹了Android開發(fā)之InetAddress基礎入門簡介,需要的朋友可以參考下2020-03-03