• HorizontalScrollView的使用演示样例


    MainActivity例如以下:
    package cc.cv;
    
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import android.app.Activity;
    import android.content.Context;
    /**
     * Demo描写叙述:
     * HorizontalScrollView的简单使用
     * 
     * 在Google文档中Gallery已经弃用.推荐使用HorizontalScrollView和ViewPager.
     * ViewPager用过不少,HorizontalScrollView倒是非常少使用.所以在此学习.
     * 
     * Demo说明:
     * 1 布局文件非常easy,在HorizontalScrollView中嵌套了一个LinearLayout
     * 2 在代码中仅仅需将自己定义的View加入到LinearLayout中就可以.
     * 这样就能够实现水平滑动了.
     *
     */
    public class MainActivity extends Activity {
    	private Context mContext;
    	private int [] mPhotosIntArray;
    	private LayoutInflater mLayoutInflater;
        private LinearLayout mGalleryLinearLayout;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		initView();
    		initData();
    	}
    
    	private void initView(){
    		mContext=this;
    		mGalleryLinearLayout=(LinearLayout) findViewById(R.id.galleryLinearLayout);
    		mLayoutInflater=LayoutInflater.from(mContext);
    	}
    	private void initData(){
    		mPhotosIntArray=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};
    	    View itemView=null;
    	    ImageView imageView=null;
    	    TextView textView;
    		for (int i = 0; i < mPhotosIntArray.length; i++) {
    			itemView=mLayoutInflater.inflate(R.layout.gallery_item, null);
    			imageView=(ImageView) itemView.findViewById(R.id.imageView);
    			textView=(TextView) itemView.findViewById(R.id.textView);
    			imageView.setImageResource(mPhotosIntArray[i]);
    			textView.setText("This is "+(i+1));
    			mGalleryLinearLayout.addView(itemView);
    		}
    	}
    
    }
    

    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" >
    
        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="150dip"
            android:layout_centerInParent="true"
            android:scrollbars="horizontal" >
    
            <LinearLayout
                android:id="@+id/galleryLinearLayout"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:gravity="center_vertical" >
            </LinearLayout>
            
        </HorizontalScrollView>
    
    </RelativeLayout>

    gallery_item.xml例如以下:
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="80dip"
            android:layout_height="80dip"
            android:layout_centerHorizontal="true" />
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/imageView"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="8dip" />
    
    </RelativeLayout>


  • 相关阅读:
    C#连接SQL Server测试
    2015结束,迎接2016
    notepad ++ 编辑 powershell profile 文件时的诡异问题
    安静的思考
    把生活过的像模像样已经很不容易
    查询SQL Server 版本信息
    一段SQL代码
    javascript面向对象编程的3种常见封装形式解析
    javascript中new操作符的原理
    关于javascript中this 指向的4种调用模式
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/6719189.html
Copyright © 2020-2023  润新知