1.HorizontalScrollView基本概念
用于布局的容器, 可以放置让用户使用滚动条查看的视图层次结构,允许视图结构比手机的屏幕大。
HorizontalScrollView是一种框架布局,这意味着你可以将包含要滚动的完整内容的子视图放入该容器; 该子视图本身也可以是具有复杂层次结构的布局管理器。一般使用横向的 LinearLayout 作为子视图,使用户可以滚动其中显示的条目.
TextView类也有其自身的滚动处理,不需要嵌入滚动视图; 但二者可以组合使用,其效果与将文本视图放入很大容器中一样。
HorizontalScrollView只支持 水平方向的滚动。如果需要用到垂直方向的滚动则可用使用 ScrollView或者ListView。
2.简单案例
Activity
Layout
显示内容布局文件
用于布局的容器, 可以放置让用户使用滚动条查看的视图层次结构,允许视图结构比手机的屏幕大。
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>