android listview半透明选择行的颜色

2024-01-11

我需要在列表视图中实现半透明的行选择,以及“按下”状态。

如果我应用纯色,那么一切都会按预期进行。但如果我应用半透明颜色(#44444444),那么我会看到默认选择颜色(在我的 2.3 android 上为橙色),在它的顶部是我的颜色(它使橙色稍微变暗)。

为什么我的下面是橙色的? 如何解决这个问题?

这是我的代码: drawable/listselectorinvisible.xml 中的选择器 xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
    android:state_selected="false"
    android:drawable="@color/transparent" />

<item android:state_pressed="true" 
    android:drawable="@color/semitransparent" />

<item android:state_selected="true" android:state_pressed="false"
    android:drawable="@color/semitransparent" />
</selector>

Listview行在layout/topscore_row.xml中定义

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" 
android:layout_height="wrap_content" 
android:layout_width="fill_parent"
android:background="@drawable/listselectorinvisible">
  <TextView android:layout_height="wrap_content" android:id="@+id/scrowNum" android:textColor="@color/fontButColor" android:text="#" android:textSize="24sp" android:layout_width="32dip" android:gravity="right|top" android:layout_gravity="top"></TextView>
  <LinearLayout android:layout_height="wrap_content" android:id="@+id/scrowNamLay" android:layout_width="142dip" android:orientation="vertical">
    <TextView android:layout_height="wrap_content" android:paddingTop="2dip" android:layout_width="fill_parent" android:id="@+id/scrowPlayer" android:textColor="@color/fontButColor" android:text="@string/tblPlayer" android:textSize="24sp" android:paddingLeft="2dip"></TextView>
    <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/scrowOpPlayer" android:textColor="@color/fontButColor" android:text="@string/tblPlayer" android:textSize="14sp" android:paddingLeft="2dip"></TextView>
  </LinearLayout>
  <ImageView android:id="@+id/scrowImg" android:layout_height="wrap_content" android:src="@drawable/small_im_bt" android:padding="2dip" android:layout_marginTop="2dip" android:layout_width="26dip"></ImageView>
  <TextView android:layout_height="wrap_content" android:layout_width="48dip" android:id="@+id/scrowScore" android:layout_marginRight="5dip" android:gravity="right" android:textColor="@color/fontButColor" android:text="@string/tblScore" android:textSize="26sp"></TextView>
  <TextView android:layout_height="wrap_content" android:id="@+id/scrowTime" android:textColor="@color/fontButColor" android:text="@string/tblTime" android:gravity="center_horizontal" android:textSize="26sp" android:layout_width="58dip"></TextView>
</LinearLayout>

最后是列表视图本身:

<ListView android:layout_width="match_parent" android:id="@+id/scoreList" android:paddingLeft="5dip" android:paddingRight="5dip" 
  android:cacheColorHint="#00000000" 
  android:choiceMode="none" 
  android:clickable="false" 
  android:focusable="false" 
  android:focusableInTouchMode="false" 
  android:longClickable="false" android:layout_height="298dip">
</ListView>

在 xml 中设置颜色失败后,我还尝试通过以下方式在我的 CustomArrayAdapter 中设置 listselectorinvisibleConvertView.setBackgroundResource(R.drawable.listselectorinvisible);但没有运气。

自定义适配器的代码:

        /* (non-Javadoc)
     * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View v = convertView;
        if (v == null) 
        {
            LayoutInflater vi = (LayoutInflater)app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.topscore_row, null);
        }

        Score score = objects.get(position);
        int color = getColor(position, score);

        if (score != null) 
        {
            ImageView iv = (ImageView) v.findViewById(R.id.scrowImg);
            if (iv != null) 
            {
                iv.setImageResource(imgs[score.gameType]);
            }
            TextView tv = (TextView) v.findViewById(R.id.scrowNum);
            tv.setText(Integer.toString(position+1) + ".");
            tv.setTypeface(app.mainFont);
            tv.setTextColor(color);
            .... ....               
        }

        v.setBackgroundResource(R.drawable.listselectorinvisible);

        return v;

    }               

先感谢您。


你必须设置android:listSelector="@drawable/listcolor"在列表视图中

你的ListView将是

<ListView android:layout_width="match_parent" android:id="@+id/scoreList" android:paddingLeft="5dip" android:paddingRight="5dip" 
  android:cacheColorHint="#00000000" 
  android:choiceMode="none" 
  android:clickable="false" 
  android:focusable="false" 
  android:focusableInTouchMode="false" 
  android:longClickable="false" 
android:listSelector="@drawable/listcolor"
android:layout_height="298dip">
</ListView>

看看下面的 URl

如何设置ListView的选择颜色 http://android-codes-examples.blogspot.com/2011/03/customized-listview-items-selection.html

谢谢 迪帕克

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android listview半透明选择行的颜色 的相关文章

随机推荐

  • 使用 shell 脚本删除服务器中的文件

    对所有文件执行 wget 后如何删除服务器中的文件 我知道当您通过 ftp 连接到服务器时 删除命令可以工作 但我无法在运行 shell 脚本时使用该命令 以下是我的脚本 请告诉我其中有什么问题 bin bash filelist ls 2
  • 如何使用 mongoimport 导入 json 文件

    我正在尝试使用导入 json 文件mongoimport使用此命令行 mongoimport db posts collection rows file post history json 但它返回此错误 connected to loca
  • 在同一查询中使用 DISTINCT 和 TOP

    我想用DISTINCT and TOP在同一个查询中 我试过 SELECT DISTINCT TOP 10 FROM TableA 但我还有一个副本personID 所以我想这样做 SELECT DISTINCT personID TOP
  • UIPickerView 最佳实践?

    一个简短的问题 在注册过程中 我想要求用户从值列表中选择一个值 使用视图控制器添加所有文本字段并为值添加选择器视图是正确的方法吗 由于选择器视图在文本字段区域之间需要如此多的空间 我想知道在这种情况下的最佳实践是什么 到目前为止 这是我的代
  • 使用可选的非默认参数以编程方式创建 R 函数

    继从如何以编程方式创建 R 函数 https stackoverflow com questions 12982528 how to create an r function programmatically 我想从它们的组件构建 R 函数
  • 响应式图像如何与“尺寸”中提供的长度“em”一起使用?

    浏览器如何理解em在响应式图像中使用时的单位 img alt A giraffe src giraffe jpg 这是有效的 并且我没有在浏览器控制台中看到警告 但如果图像预加载器的全部目的是获取图像beforeCSS被下载并解析 浏览器用
  • android BluetoothChat 使用来自另一个类的发送接收方法

    您好 我使用 BluetoothChat 示例来与 arduino 进行通信 到目前为止 一切正常 现在我想访问 sendMessage 方法 以便能够不从主类发送数据 而是从我创建的另一个类发送数据 这是BluetoothChat jav
  • Go语言中比较数组

    如何正确比较 Go 中的两个数组 例如 如何将二维数组与int条目 或任何其他类型 这种比较有多深 要比较两个数组 请使用比较运算符 http golang org ref spec Comparison operators or 引用自链
  • 我应该注入执行算法所需的对象吗?我应该注射所有东西吗?

    也许我在文档中错过了它 但我想知道应该如何处理 帮助对象 代码示例 public Path dijkstra Node startNode Node endNode Set
  • 哪种性能最好:使用 XPath 的 XPathNavigator 与使用查询的 Linq to Xml?

    我有一个应用程序 在其中使用 XPathNavigator 来迭代节点 它运行良好 但我想知道如果我使用 LINQ to Xml 我将获得什么好处 性能 可维护性 使用 XPath LINQ to Xml 对性能有何影响 我正在使用 C n
  • 将静态 IP 地址与 Amazon EC2 结合使用

    我想将 Amazon Web Service 免费微实例用于我的不同项目以进行测试和个人目的 但我需要一些静态公共 IP 来运行我的服务器 那可能吗 我可以从哪里购买 IP 并将其与我的 AWS 一起使用 EC2 弹性 IP 地址 http
  • ActiveAdmin不会保存有很多并且属于很多字段

    我有 2 个型号 类别和帖子 它们使用 has many and belongs to many 关系进行连接 我检查了 Rails 控制台 关系正常 我在 activeadmin 中创建了复选框来使用此表单字段设置帖子类别 f input
  • 为什么 %prec 在此 bison 语法中不起作用?

    考虑以下 Bison 语法 这是从我正在研究的一个更大的语法中剥离出来的 token ident left left CALLPREC start add expr ident call add call expr prec CALLPRE
  • 在 Mono 上使用自定义 SSL 客户端证书 System.Net.HttpClient

    我在用着Microsoft HTTP 客户端库 https www nuget org packages Microsoft Net Http来自 NuGet 我基本上尝试使用 X509Certificate2 证书在 HttpClient
  • lambda 内隐含的匿名类型

    In 这个问题 https stackoverflow com q 43973776 1876620 用户 Holger 提供一个答案 https stackoverflow com a 43983558 1876620这显示了我不知道的匿
  • 无法将导出用作类型,因为导出是一个值

    运行时出现此错误flow check 但我不确定这意味着什么 无法将导出用作类型 因为导出是一个值 要获取值的类型 请使用 typeof 错误位置是0 1 在 flow注释处 这是代码 flow import Chunk from mode
  • svn commit 不带-m

    我尝试过输入svn commit它将弹出一个屏幕 其中显示正在添加 更改 等的文件列表 以及在消息中输入的位置 但按 Enter 会在消息中添加换行符 而不是启动提交 是否有热键或某些组合键来启动提交 看起来底部有某种类似 emacs 的控
  • 如何以及在何处使用 AsyncStorage 保存整个 redux 存储

    我想知道是否必须执行类似的操作 才能在反应本机应用程序中使用 AsyncStorage 保存应用程序的整个存储 并在应用程序启动时检查它是否存在 var store loadInitialState async gt try var val
  • Vuetify 构建为 Web 组件样式未显示

    我正在使用 Vue 和 Vuetify 以及 Web 组件构建一个应用程序 当我将 Vuetify 添加为 Web 组件时 CSS 样式 Vuetify 消失了 我尝试将以下内容添加到 demo html 文件中但这没有帮助 我的仓库是vu
  • android listview半透明选择行的颜色

    我需要在列表视图中实现半透明的行选择 以及 按下 状态 如果我应用纯色 那么一切都会按预期进行 但如果我应用半透明颜色 44444444 那么我会看到默认选择颜色 在我的 2 3 android 上为橙色 在它的顶部是我的颜色 它使橙色稍微