【问题标题】:Dynamic Image in Android Studio TableLayoutAndroid Studio TableLayout 中的动态图像
【发布时间】:2020-05-15 11:57:34
【问题描述】:

我正在使用 Android Studio 和 Kotlin 构建一个简单的应用程序。

该应用程序显示每个月的结果为01,并以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>

我不想将结果显示为01,而是希望它是一张图片:

根据某些代码,结果是动态的?

【问题讨论】:

  • 显示您的活动或片段

标签: android tablelayout


【解决方案1】:

这是我的建议

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));
}

结果如下:

【讨论】:

猜你喜欢
  • 2011-04-19
  • 1970-01-01
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多