国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android HorizontalScrollView左右滑動效果

2019-12-12 03:44:05
字體:
來源:轉載
供稿:網友

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德格县| 高清| 上栗县| 新昌县| 大田县| 奉化市| 肥东县| 祁门县| 通许县| 辽宁省| 涞源县| 平武县| 南昌市| 凤庆县| 铜鼓县| 光山县| 台湾省| 石首市| 永登县| 涿鹿县| 黔南| 桦南县| 大余县| 依安县| 广丰县| 常熟市| 巨鹿县| 英吉沙县| 阳曲县| 延安市| 黄陵县| 福安市| 蒙城县| 彰武县| 黔江区| 泗阳县| 泾川县| 利川市| 理塘县| 临洮县| 建水县|