android - Kotlin 如何在 TableLayout 中显示图像
问题描述
我正在尝试在 TableLayout 中显示图像。图像已显示,但没有应有的行数和列数(6 行和 4 列 - 见下图!)。
这是我尝试过的
private fun drawGrid()
{
var row: TableRow
val cellSize: Size = getCellSize();
val layoutpParams: ViewGroup.LayoutParams = ViewGroup.LayoutParams(cellSize.width, cellSize.height)
var imageView: ImageView
_gridElementList = getData()
var index: Int = 0
for (i: Int in 0 until settings.row)
{
row = TableRow(this)
row.layoutParams = layoutpParams
for (j: Int in 0 until settings.column)
{
imageView = ImageView(this);
imageView.setImageResource(_gridElementList[index].drawableId)
row.addView(imageView);
index++
}
tableLayout.addView(row);
}
}
private fun getCellSize() : Size
{
windowManager.defaultDisplay.getMetrics(displaymetrics);
val screenHeight = displaymetrics.heightPixels
val screenWidth = displaymetrics.widthPixels
val height: Int = ( screenHeight / settings!!.row).toInt()
val width: Int = (screenWidth / settings!!.column).toInt()
return Size(width, height)
}
我需要调整图像大小以始终适合屏幕。矩阵最小为 2x2,最大尺寸为 4 x 8。
活动的xmls:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".GameActivity">
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/timeLabelTextView"
android:layout_width="301dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Eltelt idő"
android:textAlignment="center"
android:textSize="24sp" />
<TextView
android:id="@+id/timeTextView"
android:layout_width="168dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="0"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
</LinearLayout>
解决方案
推荐阅读
- here-api - Here.com API 返回错误的国家代码 NCY
- java - 在 Eclipse 中运行应用程序时设置 ClassPath 命令行参数
- reactjs - 如何从 React js 中的 s3 存储桶中获取 5 个最新文件?
- php - 如何使用 Laravel Socialite 登录用户(LinkedIn)
- java - 如何使用 JavaEE eclipse 将我的 React Native 应用程序连接到 MySQL 数据库?
- javascript - 如何修复硬刷新/缓存干净刷新中没有加载的数据
- python - 如何使用 Python 中的数据集使用 PSO(粒子群优化)实现聚类?
- sql - 在使用“CASE”时,是否有任何功能可以减少我的查询大小
- react-native - 是否可以从 AsyncStorage 中删除所有项目
- java - 找不到数据源 JNDI