zoukankan      html  css  js  c++  java
  • 网格布局作业

     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>
     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="20dp"
    14         android:stretchMode="columnWidth"
    15         android:gravity="center">
    16     </GridView>
    17 </LinearLayout>
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="vertical"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent">
     6 
     7     <ImageView
     8         android:layout_width="100dp"
     9         android:layout_height="100dp"
    10         android:src="@drawable/f1"
    11         android:id="@+id/gv_2"/>
    12     <TextView
    13         android:layout_width="wrap_content"
    14         android:layout_height="wrap_content"
    15         android:id="@+id/gv_3"
    16         android:text="123"
    17         android:layout_gravity="center_horizontal"
    18         android:textColor="#000"
    19         android:textStyle="bold"/>
    20 </LinearLayout>
      1 package com.example.administrator.testapp;
      2 
      3 import android.os.Bundle;
      4 import android.support.v7.app.AppCompatActivity;
      5 import android.view.View;
      6 import android.view.ViewGroup;
      7 import android.widget.BaseAdapter;
      8 import android.widget.GridView;
      9 import android.widget.ImageView;
     10 import android.widget.TextView;
     11 
     12 import java.util.ArrayList;
     13 import java.util.List;
     14 
     15 public class TestActivity10 extends AppCompatActivity {
     16 
     17     GridView gv_1;
     18     List<MyClass> liv;
     19     TextView gv_3;
     20     ImageView gv_2;
     21     @Override
     22     protected void onCreate(Bundle savedInstanceState) {
     23         super.onCreate(savedInstanceState);
     24         setContentView(R.layout.activity_test10);
     25         gv_1 = (GridView)findViewById(R.id.gv_1);
     26         gv_3 = (TextView)findViewById(R.id.tv_3);
     27         gv_2 = (ImageView)findViewById(R.id.gv_2);
     28 
     29         //1、数据 图片的id
     30         liv = new ArrayList<>();
     31         MyClass myClass = new MyClass(R.drawable.f1,"美食1");
     32         liv.add(myClass);
     33         liv.add(new MyClass(R.drawable.f2,"美食2"));
     34         liv.add(new MyClass(R.drawable.f3,"美食3"));
     35         liv.add(new MyClass(R.drawable.f4,"美食4"));
     36         liv.add(new MyClass(R.drawable.f5,"美食5"));
     37         liv.add(new MyClass(R.drawable.f6,"美食6"));
     38         liv.add(new MyClass(R.drawable.f7,"美食7"));
     39         liv.add(new MyClass(R.drawable.f8,"美食8"));
     40         ImageAdapter imageAdapter = new ImageAdapter();
     41         {
     42             gv_1.setAdapter(imageAdapter);
     43         }
     44     }
     45     class MyClass
     46     {
     47         private int img;
     48         private String content;
     49 
     50         public int getImg() {
     51             return img;
     52         }
     53 
     54         public void setImg(int img) {
     55             this.img = img;
     56         }
     57 
     58         public String getContent() {
     59             return content;
     60         }
     61 
     62         public void setContent(String content) {
     63             this.content = content;
     64         }
     65         public MyClass(int img,String content)
     66         {
     67             this.img = img;
     68             this.content = content;
     69         }
     70     }
     71     //适配器
     72     class ImageAdapter extends BaseAdapter
     73     {
     74         @Override
     75         public int getCount() {
     76             return liv.size();
     77         }
     78 
     79         @Override
     80         public Object getItem(int position) {
     81             return liv.get(position);
     82         }
     83 
     84         @Override
     85         public long getItemId(int position) {
     86             return 0;
     87         }
     88 
     89         @Override
     90         public View getView(int position, View convertView, ViewGroup parent) {
     91             //1.得到数据 图片的id
     92             MyClass myClass = liv.get(position);
     93             //2.准备View
     94             if (convertView ==null) {
     95                 convertView = View.inflate(TestActivity10.this, R.layout.gridlayout, null);
     96             }
     97             //3.适配
     98             //转换
     99             ImageView imageView = (ImageView)convertView.findViewById(R.id.gv_2);
    100             imageView.setImageResource(myClass.getImg());
    101 
    102             TextView textView = (TextView)convertView.findViewById(R.id.gv_3);
    103             textView.setText(myClass.getContent());
    104 
    105             return convertView;
    106         }
    107     }
    108 }

  • 相关阅读:
    js类型转换的坑
    JS自动填写分号导致的坑
    tomcat+javaWeb+spring的一个都市供求管理系统
    java学习笔记--java中的基本数组[5]
    mysql基础语法及拓展到web中的sql注入
    angular指令大全
    在chrome下-webkit-box布局的一个bug
    use redis instance in docker hub
    centos yum install oracle java
    认识Debian
  • 原文地址:https://www.cnblogs.com/TENOKAWA/p/5513390.html
Copyright © 2011-2022 走看看