`

android HorizontalScrollView学习

 
阅读更多
1.HorizontalScrollView基本概念
用于布局的容器可以放置让用户使用滚动条查看的视图层次结构,允许视图结构比手机的屏幕大。
HorizontalScrollView是一种框架布局,这意味着你可以将包含要滚动的完整内容的子视图放入该容器; 该子视图本身也可以是具有复杂层次结构的布局管理器。一般使用横向的 LinearLayout 作为子视图,使用户可以滚动其中显示的条目.

TextView类也有其自身的滚动处理,不需要嵌入滚动视图; 但二者可以组合使用,其效果与将文本视图放入很大容器中一样。

HorizontalScrollView只支持水平方向的滚动。如果需要用到垂直方向的滚动则可用使用ScrollView或者ListView。

2.简单案例
Activity
package com.example.zhy_horizontalscrollview;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	private LinearLayout mGallery;
	private int[] mImgIds;
	private LayoutInflater mInflater;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		mInflater = LayoutInflater.from(this);
		initImageData();
		initView();
	}
	// 初始化图片资源
	private void initImageData() {
		mImgIds = new int[] { R.drawable.a, R.drawable.b, R.drawable.c,
				R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g,
				R.drawable.h, R.drawable.l };
	}

	private void initView() {
		mGallery = (LinearLayout) findViewById(R.id.id_gallery);
		for (int i = 0; i < mImgIds.length; i++) {
			// 图片所使用的布局文件
			View view = mInflater.inflate(R.layout.activity_index_gallery_item,
					mGallery, false);
			// 设置图片资源
			ImageView img = (ImageView) view
					.findViewById(R.id.id_index_gallery_item_image);
			img.setImageResource(mImgIds[i]);
			TextView txt = (TextView) view
					.findViewById(R.id.id_index_gallery_item_text);
			// 设置图片说明
			txt.setText("some info");
			mGallery.addView(view);
		}
	}
}

Layout
<LinearLayout 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"
     >
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:layout_gravity="center_vertical"
        android:background="#AA444444"
        android:scrollbars="none" >

        <LinearLayout
            android:id="@+id/id_gallery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:orientation="horizontal" >
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

显示内容布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:background="@android:color/white" >

    <ImageView
        android:id="@+id/id_index_gallery_item_image"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="5dp"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/id_index_gallery_item_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/id_index_gallery_item_image"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:textColor="#ff0000"
        android:textSize="12dp" />

</RelativeLayout>



分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    HorizontalScrollView打造的相册效果

    近期看了一堂某在线IT学习的视频公开课,这里就不说名字了,省的有打广告的嫌疑。讲到了利用HorizontalScrollView仿ViewPager设计的一个简单相册。其实主要用了ViewPager缓存的思想。自已尝试实现了一个,大家可以看...

    无限滚动的HorizontalScrollView

    根据网上大牛的一些代码,自己试着写出来的一个效果,新人可以学习学习,因为我查资料的时候发现这个无限滚动的HorizontalScrollView资料很少。

    Android左右滑动显示图片HorizontalScrollView

    本人初期学习,参考学习写了个demo,赚点资源分下载其他大神的东西,AS编辑,勿喷

    Android利用HorizontalScrollView仿ViewPager设计简单相册

    最近学习了一个视频公开课,讲到了利用HorizontalScrollView仿ViewPager设计的一个简单相册,其实主要用了ViewPager缓存的思想。此篇文章参考:Android自定义HorizontalScrollView打造超强Gallery效果(这篇文章与...

    Android仿网易客户端顶部导航栏效果

    最近刚写了一个网易客户端首页导航条的动画效果,现在分享出来给大家学习学习。我说一下这个效果的核心原理。下面是效果图:  首先是布局,这个布局是我从网易客户端反编译后弄来的。大家看后应该明白,布局文件...

    Android一个Demo搞定所有控件

    本人在学习基础控件的时候写的,运行环境是在Android 4.2。 包含Fragment和viewPager和HorizontalScrollView,actionBar等比较新的控件的使用方法

    mooc_android_lesson8_图片浏览器

    根据本讲学习内容,学会运用HorizontalScrollView、LinearLayout进行界面布局,掌握HorizontalScrollView的应用 (1)创建一个简单的图片浏览器。 (2) 提交的作业包中含有应用程序运行效果截图。

    ViewPager实现Gallery

    在学习Gallery控件时,IDE提示已经过期,This class is deprecated.This widget is no longer supported. Other horizontally scrolling widgets include HorizontalScrollView and ViewPager from the support ...

    TabLayoutSummaryy:三种实现tablayout加viewpager滑动的实现总结

    三种实现TabLayout滑动布局方式总结自定义HorizontalScrollView实现TabLayout效果先放效果:**使用心得:**参考的网上的写法,以前我有几个项目就用的这种实现方式,实现代码详细,便于我们理解实现细节,并且个性化...

Global site tag (gtag.js) - Google Analytics