android中一个关于horizontalscrollview的例子

本文介绍了如何在Android中使用HorizontalScrollView组件创建横向滑动的界面。重点指出该组件只能添加一个子组件,并提供了MainActivity.java、activity_main.xml和scrollview_item.xml的相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

horizontalscrollview这个组件主要是用来实现横向滑动界面显示的,其中注意一点的是:该组件只能添加一个子组件。

先来结果图:

 

代码如下:

主activity(MainActivity.java):

package com.tody.horizontalscrollviewdemo;

import android.os.Bundle;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 主界面
 * 
 * @author tody.yang
 * 
 */
public class MainActivity extends Activity {

	private LinearLayout mLinearLayout;
	private int mWidth;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		getDeviceWidth();
		initView();
	}

	/**
	 * 获得屏幕宽度
	 */
	private void getDeviceWidth() {
		DisplayMetrics dm = new DisplayMetrics();// 获得屏幕分辨率
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		mWidth = dm.widthPixels;
	}

	/**
	 * 初始化组件
	 */
	private void initView() {
		int[] imageArray = { R.drawable.d0, R.drawable.d1, R.drawable.d2,
				R.drawable.d3, R.drawable.d4, R.drawable.d5 };// 图片数组(长度和horizontalscrollview的子项长度一样)
		mLinearLayout = (LinearLayout) findViewById(R.id.scrollview_layout);// 实例化线性布局
		mLinearLayout.removeAllViews();// 删除以前的组件(如此保证每次都是horizontalscrollview中只有6个组件)
		for (int i = 0; i < 6; i++) {// 在horizontalscrollview中添加6个组件
			int width = mWidth / 3;
			LinearLayout itemLayout = (LinearLayout) LinearLayout.inflate(
					MainActivity.this, R.layout.scrollview_item, null);// 动态实例化一个LinearLayout
			itemLayout.setLayoutParams(new LayoutParams(width,
					LayoutParams.MATCH_PARENT));// 设置宽度为一张屏幕显示三个组件
			itemLayout.setGravity(Gravity.CENTER_VERTICAL);// 设置垂直居中

			ImageView mImageView = (ImageView) itemLayout
					.findViewById(R.id.imageview);
			TextView mTextView = (TextView) itemLayout
					.findViewById(R.id.textview);

			final String page = "第" + (i + 1) + "张";

			mTextView.setText(page);
			mImageView.setBackgroundResource(imageArray[i]);

			mLinearLayout.addView(itemLayout);

			itemLayout.setOnTouchListener(new OnTouchListener() {// 每一项添加触摸事件

						public boolean onTouch(View v, MotionEvent event) {
							// TODO Auto-generated method stub
							Toast.makeText(MainActivity.this, "点击了" + page,
									Toast.LENGTH_SHORT).show();
							return false;
						}
					});
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


主activity的布局文件(activity_main.xml):

<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=".MainActivity" >

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <!-- 因为HorizontalScrollView只允许加载一个子组件,所有这里定义一个线性布局作为唯一的子组件,而我们想添加的其他组件可以通过动态加载布局,这样给程序的感觉还是一个子组件(ID为scrollview_layout的组件) -->

        <LinearLayout
            android:id="@+id/scrollview_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical" >
        </LinearLayout>
    </HorizontalScrollView>

</RelativeLayout>

动态加载的布局文件(scrollview_item.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="俊哥" />

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/d0" />

</LinearLayout>



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值