4

这是我的编码部分:

列表视图.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="fill_parent"
android:orientation="vertical">


<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:divider="#FFFFFF"
    android:dividerHeight="7dp"
     android:layout_weight="1"
    android:listSelector="@drawable/list_selector" />


 </LinearLayout>    

list_selector.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

 <item 
android:state_selected="false"
android:state_pressed="false" 

android:drawable="@drawable/gradient_bg" />
<item android:state_pressed="true" 
android:drawable="@drawable/gradient_bg_hover" />
<item android:state_selected="true"
android:state_pressed="false" 
android:drawable="@drawable/gradient_bg_hover" />
</selector>

渐变bg.xml:

<?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
  android:startColor="#e8eef4"
  android:centerColor="#fdfdfd"
  android:endColor="#d3dfe8"
  android:angle="270" />
        <stroke android:width="1dp" android:color="#b7ccdd" />

    <padding android:left="6dp" android:top="6dp" android:right="6dp"
            android:bottom="6dp" />
     <margin android:left="15dp" android:top="15dp" android:right="15dp"
            android:bottom="15dp" />
        <corners android:radius="4dp" />
    </shape>

gradient_bg_hover.xml:

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

  <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
  android:startColor="#ffe181"
  android:centerColor="#f5cd4a"
  android:endColor="#e8c34e"
  android:angle="270" />
        <stroke android:width="1dp" android:color="#e3b52c" />

    <padding android:left="6dp" android:top="6dp" android:right="6dp"
            android:bottom="6dp" />
     <margin android:left="15dp" android:top="15dp" android:right="15dp"
            android:bottom="15dp" />
        <corners android:radius="4dp" />
     </shape>

在这里我得到了 o/p : 在此处输入图像描述

但我希望需要像这样的o/p: 在此处输入图像描述

请比较两个图像上的悬停状态并为此提供解决方案..

4

4 回答 4

6

列表视图.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="fill_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#FFFFFF"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

</LinearLayout>

list_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->
<item 
 android:state_selected="false"
    android:state_pressed="false" 
    android:drawable="@drawable/gradient_bg" />
<item android:state_pressed="true" 
    android:drawable="@drawable/gradient_bg_hover" />
<item android:state_selected="true"
 android:state_pressed="false" 
    android:drawable="@drawable/gradient_bg_hover" />
</selector>

渐变bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <!--  Gradient Bg for listrow -->
  <gradient
      android:startColor="#f1f1f2"
      android:centerColor="#e7e7e8"
      android:endColor="#cfcfcf"
      android:angle="270" />
</shape>

gradient_bg_hover.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <!-- Gradient BgColor for listrow Selected -->
  <gradient
      android:startColor="#18d7e5"
      android:centerColor="#16cedb"
      android:endColor="#09adb9"
      android:angle="270" />
</shape>
于 2012-10-26T08:57:19.993 回答
2

您没有使用聚焦状态尝试使用以下代码

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--  Active tab -->
    <item android:state_selected="true" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/gradient_bg_hover" />
    <!--  Inactive tab -->
    <item android:state_selected="false" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/gradient_bg" />
    <!--  Pressed tab -->
    <item android:state_pressed="true" android:drawable="@drawable/gradient_bg_hover" />
    <!--  Selected tab (using d-pad) -->
    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/gradient_bg_hover" />
</selector>
于 2012-10-26T08:55:53.437 回答
0

迟到了,但为了其他:)

您需要将列表视图的选择器设置为透明色

listView.setSelector(R.color.transparent);

在 color.xml 只需添加以下内容

<color name="transparent">#00000000</color>
于 2014-11-07T16:04:46.037 回答
0

从 gradient_bg_hover.xml 和 gradient_bg.xml 中删除填充

而是将填充放在 ListView 中,例如:

<ListView
    android:id="@+id/Sales_List"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    **android:paddingLeft="6dp"
    android:paddingTop="6dp"
    android:paddingRight="6dp"
    android:paddingBottom="6dp"**
    android:dividerHeight="7dp"
    android:divider="@android:color/transparent"
    android:listSelector="@drawable/listviewselect">

它应该工作

于 2016-02-24T13:02:08.427 回答