[android]代码库
1.MainActivity.java
package com.example.acer.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private LinearLayout scrollView;
private int []array;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = (LinearLayout) findViewById(R.id.scrollView);
initData();
}
private void initData() {
array = 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};
for (int i = 0; i < 8; i++) {
ImageView imageView=new ImageView(this);
imageView.setImageResource(array[i]);
scrollView.addView(imageView);
}
}
}
2.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" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="250dip"
android:scrollbars="horizontal"
android:layout_centerVertical="true">
<LinearLayout
android:id="@+id/scrollView"
android:layout_width="150dp"
android:layout_height="300dp"
android:orientation="vertical"
android:gravity="center_vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>