運(yùn)行效果:
xml代碼
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".QQPicture"
android:id="@+id/layout"
android:orientation="horizontal"
android:rowCount="3"
android:columnCount="4"
>
</GridLayout>
主函數(shù)代碼
public class QQPicture extends AppCompatActivity {
private ImageView[] img=new ImageView[12];
private int[] imagePath = new int[]{
R.mipmap.p1,R.mipmap.p2,R.mipmap.p3,R.mipmap.p4,R.mipmap.p5,R.mipmap.p6
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qqpicture);
GridLayout layout=(GridLayout)findViewById(R.id.layout);
for (int i = 0; i < imagePath.length; i++) {
img[i]=new ImageView(QQPicture.this);
img[i].setImageResource(imagePath[i]);
img[i].setPadding(2,2,2,2);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(116,68);
img[i].setLayoutParams(params);
layout.addView(img[i]);
}
}
}