android mani fest
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.administrator.testapp"> 4 5 <application 6 android:allowBackup="true" 7 android:icon="@mipmap/ic_launcher" 8 android:label="@string/app_name" 9 android:supportsRtl="true" 10 android:theme="@style/AppTheme"> 11 <activity android:name=".MainActivity" /> 12 <activity android:name=".test_activity6" /> 13 <activity android:name=".TestActivity7" /> 14 <activity android:name=".TestActivity8" /> 15 <activity android:name=".Main2Activity" /> 16 <activity android:name=".TestActivity9"> 17 </activity> 18 <activity android:name=".TestActivity10"> 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 <category android:name="android.intent.category.LAUNCHER" /> 22 </intent-filter> 23 </activity> 24 </application> 25 26 </manifest>
activity_test10.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context="com.example.administrator.testapp.TestActivity10"> 7 8 <GridView 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" 11 android:id="@+id/gv_1" 12 android:numColumns="4" 13 android:horizontalSpacing="30dp" 14 android:stretchMode="columnWidth" 15 android:gravity="center" 16 > 17 </GridView> 18 19 </LinearLayout>
TestActivity10.java
1 package com.example.administrator.testapp; 2 3 import android.app.ActionBar; 4 import android.media.Image; 5 import android.support.v7.app.AppCompatActivity; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.BaseAdapter; 10 import android.widget.GridView; 11 import android.widget.ImageView; 12 13 import java.util.ArrayList; 14 import java.util.List; 15 16 public class TestActivity10 extends AppCompatActivity { 17 18 GridView gv_1; 19 20 List<Integer>liv; 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_test10); 26 27 GridView gv_1=(GridView)findViewById(R.id.gv_1); 28 29 //1-数据Data 图片id 30 liv = new ArrayList<>(); 31 liv.add(R.drawable.f1); 32 liv.add(R.drawable.f2); 33 liv.add(R.drawable.f3); 34 liv.add(R.drawable.f4); 35 liv.add(R.drawable.f5); 36 liv.add(R.drawable.f6); 37 liv.add(R.drawable.f7); 38 liv.add(R.drawable.f8); 39 40 gv_1.setAdapter(new imageadapter()); 41 } 42 //适配器 43 44 class imageadapter extends BaseAdapter 45 { 46 @Override 47 public int getCount() { 48 return liv.size(); 49 } 50 51 @Override 52 public Object getItem(int position) { 53 return liv.get(position); 54 } 55 56 @Override 57 public long getItemId(int position) { 58 return 0; 59 } 60 61 @Override 62 public View getView(int position, View convertView, ViewGroup parent) { 63 //1-得到数据 图片的id 64 Integer ivid = liv.get(position); 65 //2-准备视图(View) 66 if (convertView==null) { 67 convertView=new ImageView(TestActivity10.this); 68 } 69 //3-适配 70 //转换 71 ImageView imageview = (ImageView)convertView; 72 imageview.setImageResource(ivid); 73 // imageview.setMaxWidth(50); 74 // imageview.setMaxHeight(50); 75 imageview.setLayoutParams(new GridView.LayoutParams(200,200)); 76 return imageview; 77 } 78 } 79 80 }