java - 我在 ListView 中有一个自定义单元格。如何使其在触摸时突出显示?

标签 java android android-listview

我希望 ListView 中的单元格在触摸时突出显示(就像它们通常对未修改的 ListView 所做的那样)。如何对我修改过的单元格执行此操作?

这是我的手机的样子:

enter image description here

这是我的单元格的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">

  <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="34dip"
        android:stretchColumns="0"
        android:divider="@null"       
         >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/plaincell"
            android:divider="@null"  >

            <TextView
                android:id="@+id/txtKey"
                android:text="Some text"
                android:textSize="16dip"
                android:textStyle="bold"
                android:layout_marginLeft="8dip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="left|center_vertical"
                android:layout_weight="1.0"
                android:layout_marginTop="6dp"
                android:layout_marginBottom="6dp"
                android:textColor="@color/black"
            />

            <TextView
                android:id="@+id/txtValue"
                android:text="Some text"
                android:textSize="16dip"
                android:layout_marginRight="8dip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="right|center_vertical"
                android:layout_weight="1.0"
                android:layout_marginTop="6dp"
                android:layout_marginBottom="6dp"
                android:textColor="@color/darkblue"
            />

        </TableRow>

  </TableLayout>

</LinearLayout>

最佳答案

你的背景绘制应该是 state list对于不同的状态有不同的图形(例如,按下、聚焦、两者都不是)。

编辑:要使用纯色,您可以执行以下操作:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@color/pressedcolor" android:state_pressed="true" />
   <item android:drawable="@color/unpressedcolor" />
</selector>

然后您可以在资源文件中定义自己的颜色:

<resources>
    <color name="pressedcolor">#ff0000</color>
    <color name="unpressedcolor">#0000ff</color>
</resources>

关于java - 我在 ListView 中有一个自定义单元格。如何使其在触摸时突出显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12573648/

相关文章:

android - 将新项目添加到 ListView 的开头,并保持滚动位置

java - 在构造函数之外使用 "setVisible"的目的是什么?

java - 有什么理由不应该将我的 keystore 存储在版本控制中?

Travis CI 上的 javax.net.ssl.SSLHandshakeException

android - Instanceof 不返回正确答案

android - 使水平 ListView 的列表项的高度动态

java - 您可以向 android XML 添加额外的字符串/整数变量吗?

java - 禁用 JIT 和执行时间

android cardview 显示卡周围的边框

java - 日期值不正确