如何使用 Kotlin 在 Android 应用中创建 GridView 布局?
此示例演示了如何使用 Kotlin 在 Android 应用中创建 GridView 布局。
步骤 1 - 在 Android Studio 中创建一个新项目,转到文件?新建项目并填写所有必需的详细信息以创建新项目。
步骤 2 - 将以下代码添加到 res/layout/activity_main.xml。
示例
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".MainActivity"> <GridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="2" /> </RelativeLayout>
步骤 3 - 将以下代码添加到 src/MainActivity.kt
import android.os.Bundle import android.widget.AdapterView.OnItemClickListener import android.widget.GridView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { lateinit var gridView: GridView private var playerNames = arrayOf("Cristiano Ronaldo", "Joao Felix", "Bernado Silva", "Andre Silve", "Bruno Fernandez", "William Carvalho", "Nelson Semedo", "Pepe", "Rui Patricio") private var playerImages = intArrayOf(R.drawable.ronaldo, R.drawable.felix, R.drawable.bernado, R.drawable.andre, R.drawable.bruno, R.drawable.carvalho, R.drawable.semedo, R.drawable.pepe, R.drawable.patricio) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" gridView = findViewById(R.id.gridView) val mainAdapter = MainAdapter(this@MainActivity, playerNames, playerImages) gridView.adapter = mainAdapter gridView.onItemClickListener = OnItemClickListener { _, _, position, _ -> Toast.makeText(applicationContext, "You CLicked " + playerNames[+position], Toast.LENGTH_SHORT).show() } } }
步骤 4 - 创建一个 Kotlin 类 (MyAdapter.kt) 并添加以下代码
import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.BaseAdapter import android.widget.ImageView import android.widget.TextView internal class MainAdapter( private val context: Context, private val numbersInWords: Array<String>, private val numberImage: IntArray ) : BaseAdapter() { private var layoutInflater: LayoutInflater? = null private lateinit var imageView: ImageView private lateinit var textView: TextView override fun getCount(): Int { return numbersInWords.size } override fun getItem(position: Int): Any? { return null } override fun getItemId(position: Int): Long { return 0 } override fun getView( position: Int, convertView: View?, parent: ViewGroup ): View? { var convertView = convertView if (layoutInflater == null) { layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater } if (convertView == null) { convertView = layoutInflater!!.inflate(R.layout.rowitem, null) } imageView = convertView!!.findViewById(R.id.imageView) textView = convertView.findViewById(R.id.textView) imageView.setImageResource(numberImage[position]) textView.text = numbersInWords[position] return convertView } }
步骤 5 - 创建一个布局资源文件 (row_item.xml) 并添加以下代码 -
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center" android:padding="8dp"> <ImageView android:id="@+id/imageView" android:layout_width="100dp" android:layout_height="100dp" /> <TextView android:textAlignment="center" android:gravity="center" android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="Numbers" android:layout_marginBottom="10dp" android:textColor="@android:color/background_dark" android:textSize="24sp" android:textStyle="bold" /> </LinearLayout>
步骤 6 - 将以下代码添加到 androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.q11"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
让我们尝试运行您的应用程序。我假设您已将您的实际 Android 移动设备连接到您的计算机。要从 Android Studio 运行应用程序,请打开项目的某个 activity 文件,然后点击工具栏上的运行图标 。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕
广告