Android HorizontalScrollView左右滑動(dòng)效果
本文實(shí)例為大家分享了Android HorizontalScrollView左右滑動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
一.什么是HorizontalScrollView
HorizontalScrollView實(shí)際上是一個(gè)FrameLayout ,這意味著你只能在它下面放置一個(gè)子控件 ,這個(gè)子控件可以包含很多數(shù)據(jù)內(nèi)容。有可能這個(gè)子控件本身就是一個(gè)布局控件,可以包含非常多的其他用來(lái)展示數(shù)據(jù)的控件。這個(gè)布局控件一般使用的是一個(gè)水平布局的LinearLayout。TextView也是一個(gè)可滾動(dòng)的視圖控件,所以一般不需要HorizontalScrollView一般通過(guò)放置一個(gè)LinearLayout子控件。如果要使其添加其他的控件,就使用LinearLayout子控件來(lái)添加其他的控件,最后達(dá)到豐富其內(nèi)容的效果。
二.使用HorizontalScrollView實(shí)現(xiàn)左右滑動(dòng)的效果
1.編寫(xiě)布局文件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)開(kāi)始添加數(shù)據(jù)
7.尋找行布局,第一個(gè)參數(shù)為行布局ID,第二個(gè)參數(shù)為這個(gè)行布局需要放到那個(gè)容器上
8.通過(guò)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); //開(kāi)始添加數(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); //通過(guò)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自定義View實(shí)現(xiàn)比賽時(shí)間閃動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)比賽時(shí)間閃動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03android通過(guò)代碼的形式來(lái)實(shí)現(xiàn)應(yīng)用程序的方法
因?yàn)閼?yīng)用程序的安裝與卸載模塊在android系統(tǒng)中已經(jīng)寫(xiě)好了,所以我們只需要激活就行了2013-10-10Android WorkManager實(shí)現(xiàn)后臺(tái)定時(shí)任務(wù)流程詳解
WorkManager是Android Jetpack的一個(gè)強(qiáng)大的組件,用于處理后臺(tái)耗時(shí)任務(wù)。后臺(tái)任務(wù)可以是一次性的,也可以是重復(fù)的,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01Android 中RecyclerView頂部刷新實(shí)現(xiàn)詳解
這篇文章主要介紹了Android 中RecyclerView頂部刷新實(shí)現(xiàn)詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-10-10android實(shí)現(xiàn)可以滑動(dòng)的平滑曲線圖
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)可以滑動(dòng)的平滑曲線圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Android開(kāi)發(fā)之InetAddress基礎(chǔ)入門(mén)簡(jiǎn)介與源碼實(shí)例
這篇文章主要介紹了Android開(kāi)發(fā)之InetAddress基礎(chǔ)入門(mén)簡(jiǎn)介,需要的朋友可以參考下2020-03-03Android GridView實(shí)現(xiàn)橫向列表水平滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android GridView實(shí)現(xiàn)橫向列表水平滾動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07簡(jiǎn)單實(shí)用的Android UI微博動(dòng)態(tài)點(diǎn)贊效果
這篇文章主要為大家詳細(xì)介紹了簡(jiǎn)單實(shí)用的Android UI微博動(dòng)態(tài)點(diǎn)贊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10