【问题标题】:Change the background of a table row onClickonClick 更改表格行的背景
【发布时间】:2022-08-15 14:17:42
【问题描述】:

我在 Android Studio 中有一个表格,当我单击一个单元格时,它应该变成绿色或任何其他颜色,这并不重要,但我不知道如何实现它。 我现在只有 xml 文件,我不确定要添加的确切内容和位置。 这是我第一次尝试实现类似的东西,我不知道我应该从哪里开始,或者我需要什么。

这是xml:

<?xml version=\"1.0\" encoding=\"utf-8\"?>

<TableLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"

    android:layout_width=\"fill_parent\"
    android:layout_height=\"match_parent\"
    android:gravity=\"center\"
    android:stretchColumns=\"0,1,2\">

    <TableRow
        android:id=\"@+id/blank\"
        android:layout_width=\"fill_parent\"
        android:layout_height=\"wrap_content\"
        android:layout_margin=\"1dp\"
        android:background=\"#FFFFFF\">

        <TableRow

            android:layout_width=\"fill_parent\"
            android:layout_height=\"0dp\"
            android:layout_margin=\"1dp\"
            android:layout_weight=\"1\"
            android:background=\"#000000\">

        </TableRow>

    </TableRow>

    <TableRow
        android:layout_width=\"fill_parent\"
        android:layout_height=\"0dp\"
        android:layout_margin=\"1dp\"
        android:layout_weight=\"1\"
        android:background=\"#000000\"

        >

        <TextView
            android:layout_width=\"match_parent\"
            android:layout_height=\"match_parent\"
            android:layout_column=\"0\"
            android:layout_margin=\"1dp\"
            android:background=\"#FFFFFF\"
            android:gravity=\"center\"
            android:text=\"Text\"
            android:textAppearance=\"?android:attr/textAppearanceLarge\"
            android:textStyle=\"bold\" />

        <TextView
            android:layout_width=\"wrap_content\"
            android:layout_height=\"match_parent\"
            android:layout_column=\"1\"
            android:layout_margin=\"1dp\"
            android:background=\"#FFFFFF\"
            android:gravity=\"center\"
            android:text=\"Text\"
            android:textAppearance=\"?android:attr/textAppearanceLarge\"
            android:textStyle=\"bold\" />

        <TextView
            android:layout_width=\"wrap_content\"
            android:layout_height=\"match_parent\"
            android:layout_column=\"2\"
            android:layout_margin=\"1dp\"
            android:background=\"#FFFFFF\"
            android:gravity=\"center\"
            android:text=\"Text\"
            android:textAppearance=\"?android:attr/textAppearanceLarge\"
            android:textStyle=\"bold\" />
    </TableRow>

    <TableRow
        android:layout_width=\"fill_parent\"
        android:layout_height=\"0dp\"
        android:layout_margin=\"1dp\"
        android:layout_weight=\"1\"
        android:background=\"#000000\">

        <TextView
            android:layout_width=\"match_parent\"
            android:layout_height=\"match_parent\"
            android:layout_column=\"0\"
            android:layout_margin=\"1dp\"
            android:background=\"#FFFFFF\"
            android:gravity=\"center\"
            android:text=\" Text\"
            android:textAppearance=\"?android:attr/textAppearanceLarge\" />

        <TextView
            android:layout_width=\"wrap_content\"
            android:layout_height=\"match_parent\"
            android:layout_column=\"1\"
            android:layout_margin=\"1dp\"
            android:background=\"#FFFFFF\"
            android:gravity=\"center\"
            android:text=\" Text\"
            android:textAppearance=\"?android:attr/textAppearanceLarge\" />

        <TextView
            android:layout_width=\"wrap_content\"
            android:layout_height=\"match_parent\"
            android:layout_column=\"2\"
            android:layout_margin=\"1dp\"
            android:background=\"#FFFFFF\"
            android:gravity=\"center\"
            android:text=\" Text\"
            android:textAppearance=\"?android:attr/textAppearanceLarge\" />
    </TableRow>


    <TableRow
        android:layout_width=\"fill_parent\"
        android:layout_height=\"0dp\"
        android:layout_margin=\"1dp\"
        android:layout_weight=\"1\"
        android:background=\"#000000\">

        <TextView
            android:layout_width=\"match_parent\"
            android:layout_height=\"match_parent\"
            android:layout_column=\"0\"
            android:layout_margin=\"1dp\"
            android:background=\"#FFFFFF\"
            android:gravity=\"center\"
            android:text=\" Text\"
            android:textAppearance=\"?android:attr/textAppearanceLarge\" />

        <TextView
            android:layout_width=\"wrap_content\"
            android:layout_height=\"match_parent\"
            android:layout_column=\"1\"
            android:layout_margin=\"1dp\"
            android:background=\"#FFFFFF\"
            android:gravity=\"center\"
            android:text=\" Text\"
            android:textAppearance=\"?android:attr/textAppearanceLarge\" />

        <TextView
            android:layout_width=\"wrap_content\"
            android:layout_height=\"match_parent\"
            android:layout_column=\"2\"
            android:layout_margin=\"1dp\"
            android:background=\"#FFFFFF\"
            android:gravity=\"center\"
            android:text=\" Text\"
            android:textAppearance=\"?android:attr/textAppearanceLarge\" />
    </TableRow>


</TableLayout>

有人可以帮忙吗? 非常感谢。

  • 我们希望您诚实地尝试解决方案,发布该尝试,然后询问有关它的具体问题(即解释它为什么不起作用或它有什么问题)。

标签: java android android-studio onclick


【解决方案1】:

也许在每一行中插入一个 EditText 标记对你很有用,这样你就可以随意改变它的颜色,比如"editTextVar.setBackground('some-color-code')".

我希望为您提供最好的尝试。

【讨论】:

    【解决方案2】:

    用它:

    your_object.setBackgroundColor(getResources().getColor(R.color.your_colorID_in_colors));
    

    或者它(系统颜色):

    your_object.setBackgroundColor(Color.WHITE);
    

    【讨论】:

      猜你喜欢
      • 2011-05-23
      • 2018-10-14
      • 2017-06-22
      • 2020-12-16
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      相关资源
      最近更新 更多