HorizontalScrollView实现画廊

xml文件

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

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

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:scaleType="fitCenter"
        android:contentDescription="@string/desc" />

mainActivity

public class MainActivity extends Activity implements View.OnClickListener
{
	
	private LinearLayout myGallery;
	private ImageView showImage;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		myGallery = (LinearLayout)findViewById(R.id.mygallery);
		showImage = (ImageView)findViewById(R.id.imageView);
		
		
		Integer[] images = {R.drawable.item1, R.drawable.item2, R.drawable.item3, R.drawable.item4, R.drawable.item5};
		for(Integer id:images)
		{
			myGallery.addView(insertImage(id));
		}
	}

	@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;
	}

	private View insertImage(Integer id)
	{
		LinearLayout layout = new LinearLayout(getApplicationContext());
		layout.setLayoutParams(new LayoutParams(320, 320));
		layout.setGravity(Gravity.CENTER);
		
		ImageView imageView = new ImageView(getApplicationContext());
		imageView.setLayoutParams(new LayoutParams(300, 300));
		imageView.setBackgroundResource(id);
		imageView.setOnClickListener(this);
		imageView.setId(id);
		
		
		layout.addView(imageView);
		
		return layout;
	}

	@Override
	public void onClick(View v)
	{
		// TODO Auto-generated method stub
		ImageView imageView = (ImageView)v;
		
		showImage.setImageResource(imageView.getId());
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值