我正在使用Android Studio和Kotlin构建一个简单的应用程序。
该应用程序显示每个月的结果为“ 0”或“ 1”,并以TableLayout格式使用。
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:padding="1dip"
android:text="January" />
<TextView
android:id="@+id/JanYear1"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:padding="1dip"
android:text="0" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:padding="1dip"
android:text="@string/februrary" />
<TextView
android:id="@+id/FebYear1"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:padding="1dip"
android:text="1" />
</TableRow>
而不是将结果显示为“ 0”或“ 1”,我希望它是图像:enter image description here或enter image description here,并且结果根据某些代码是动态的?
任何帮助将不胜感激。
欢呼声
[这是我的主张
XML
<TableLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#BBFFAA"
android:text="MONTH"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#AACCBB"
android:text="YEAR"
android:textSize="20dp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
JAVA
TableLayout tableLayout = findViewById(R.id.tabLayout);
String[][] months = {{"janvier", "0"}, {"February", "1"}, {"March", "0"}, {"April", "1"},
{"May", "1"}, {"June", "1"}, {"July", "1"}, {"August", "1"},
{"September", "1"}, {"October", "1"}, {"November", "1"}, {"December", "1"}};
for (int i = 0; i < months.length; i++) {
TextView month = new TextView(this);
TextView year = new TextView(this);
TableRow tableRow = new TableRow(this);
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
tableRow.setLayoutParams(layoutParams);
layoutParams.weight = 1;
month.setText(months[i][0]);
month.setLayoutParams(layoutParams);
tableRow.addView(month);
//
if ((months[i][1].equals("1"))) {
year.setBackgroundResource(R.drawable.ok);
//
tableRow.addView(year);
} else if (months[i][1].equals("0")) {
year.setBackgroundResource(R.drawable.no);
tableRow.addView(year);
}
tableLayout.addView(tableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
这里是结果: