Java 我在列表视图中有一个自定义单元格。如何在触摸屏上突出显示?

Java 我在列表视图中有一个自定义单元格。如何在触摸屏上突出显示?,java,android,android-listview,Java,Android,Android Listview,我希望我的ListView中的单元格在触摸时高亮显示(就像它们在未修改的ListView中通常所做的那样)。我如何使用我修改过的单元格 以下是我的手机的外观: 这是我手机的XML: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent

我希望我的ListView中的单元格在触摸时高亮显示(就像它们在未修改的ListView中通常所做的那样)。我如何使用我修改过的单元格

以下是我的手机的外观:

这是我手机的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>

将线性布局的背景设置为可绘制的选择器

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

其中myClickedStateDrawable、myNonClickedStateDrawable是定义布局背景的Drawable


希望这有助于

使线性布局的背景成为可绘制的选择器

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

其中myClickedStateDrawable、myNonClickedStateDrawable是定义布局背景的Drawable


希望这对您有所帮助

您的背景绘图应该是针对不同状态具有不同图形(例如,按下、聚焦或两者都没有)的

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

<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>

#ff0000
#0000ff

您的背景绘图应该是一个在不同状态下具有不同图形的图形(例如,按下、聚焦或两者都没有)

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

<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>

#ff0000
#0000ff

我知道发生了什么,但首先:

  • 大多数只有一个子级的布局都可以删除
  • 此外,TableLayout和TableRow都基于LinearLayout类
所以实际上你有三个线性布局,你只需要一个

问题:
我假设这一行设置了覆盖整个布局的新背景色:

android:background="@drawable/plaincell"
这还包括或覆盖默认的ListView颜色选择器。

您可能需要查看是否正在更改屏幕上每个UI元素的背景和文本颜色属性。如果在主题中设置这些属性,则不需要创建新的颜色选择器来执行旧的颜色选择器已经执行的操作。

我知道发生了什么,但首先

  • 大多数只有一个子级的布局都可以删除
  • 此外,TableLayout和TableRow都基于LinearLayout类
所以实际上你有三个线性布局,你只需要一个

问题:
我假设这一行设置了覆盖整个布局的新背景色:

android:background="@drawable/plaincell"
这还包括或覆盖默认的ListView颜色选择器。

您可能需要查看是否正在更改屏幕上每个UI元素的背景和文本颜色属性。如果在主题中设置这些属性,则不需要创建新的颜色选择器来执行旧的颜色选择器已经执行的操作。

是否无法仅使用简单的纯色来执行此操作?当然。将选择器与非聚焦颜色和其他所有颜色一起使用。您不必使用形状。实际上,格式应为“”。你可以单独定义你的颜色资源。没有办法只用简单的纯色来定义吗?当然。将选择器与非聚焦颜色和其他所有颜色一起使用。您不必使用形状。实际上,格式应为“”。你可以单独定义你的颜色资源。没有办法只使用简单的纯色吗?在我链接到的同一页的其他地方:“注意:颜色资源也可以用作XML中的可绘制资源。例如,在创建可绘制状态列表时,你可以引用android:drawable属性的颜色资源(android:drawable=“@color/green”)。因此,您可以使用纯色作为每个项目的可绘制颜色。是否无法仅使用简单的纯色进行绘制?来自我链接到的同一页面的其他地方:“注意:颜色资源也可以用作XML中的可绘制属性。例如,在创建可绘制状态列表时,可以为android:drawable属性(android:drawable=“@color/green”)”引用颜色资源。因此,您可以将纯色用作每个项目的可绘制属性。