listview android:cacheColorHint,listSelector属性作用 分享

本文介绍如何在自定义ListView时去除默认选中项的背景色,包括设置android:cacheColorHint和android:listSelector属性的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义listview的时候,当你不使用android:cacheColorHint=“#00000000”会出现下面选中一个空间黑色底色的情况,破坏整体美观度:

当你不使用android:listSelector属性,默认会显示选中的item为橙黄底色,有时候我们需要去掉这种效果

android:cacheColorHint="#00000000" android:listSelector="@android:color/transparent"

FanHui.java:package com.videogo.ui.login;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import com.videogo.openapi.EZOpenSDK;import ezviz.ezopensdk.R;import androidx.appcompat.app.AppCompatActivity;public class FanHui extends AppCompatActivity { private static final String TAG = “EZPreview”; private String mAppKey; private String mDeviceSerial; private String mVerifyCode; private String mAccessToken; private int mCameraNo; private static final String KEY_APPKEY = “appkey”; private static final String KEY_SERIAL = “serial”; private static final String KEY_VERIFYCODE = “VerifyCode”; private static final String KEY_ACCESSTOKEN = “accessToken”; private static final String KEY_CAMERANO = “cameraNo”; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.ez_playback_list_page); extractParametersFromIntent(); View fanHui = findViewById(R.id.fanhui); fanHui.setOnClickListener(v -> finish()); Button huifangBtn = findViewById(R.id.fanhui); huifangBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 创建Intent跳转到FanHui活动 Intent intent = new Intent(FanHui.this, MainActivity.class); // 传递必要参数(可选) intent.putExtra(“deviceSerial”, mDeviceSerial); intent.putExtra(“cameraNo”, mCameraNo); intent.putExtra(“accessToken”, mAccessToken); intent.putExtra(“appkey”, mAppKey); intent.putExtra(“verifyCode”, mVerifyCode); startActivity(intent); } }); } private void extractParametersFromIntent() { Bundle extras = getIntent().getExtras(); if (extras != null) { mAppKey = extras.getString(KEY_APPKEY, “”); mDeviceSerial = extras.getString(KEY_SERIAL, “”); mVerifyCode = extras.getString(KEY_VERIFYCODE, “”); mAccessToken = extras.getString(KEY_ACCESSTOKEN, “”); mCameraNo = extras.getInt(KEY_CAMERANO, 0); Log.d(TAG, “Received parameters:”); Log.d(TAG, “AppKey: " + mAppKey); Log.d(TAG, “DeviceSerial: " + mDeviceSerial); Log.d(TAG, “VerifyCode: " + mVerifyCode); Log.d(TAG, “AccessToken: " + mAccessToken); Log.d(TAG, “CameraNo: " + mCameraNo); } else { Log.e(TAG, “No parameters received from intent”); // 如果没有参数,可以显示错误信息并退出// finish(); } }} ez_playback_list_page.xml: <com.videogo.widget.TitleBar android:id=”@+id/title” android:layout_width=“match_parent” android:layout_height=“wrap_content”> </com.videogo.widget.TitleBar> <com.videogo.widget.TitleBar android:id=”@+id/pb_title_bar_landscape” android:layout_width=“match_parent” android:layout_height=“80dp” android:visibility=“gone” /> <com.videogo.widget.loading.LoadingView android:layout_width=“wrap_content” android:layout_height=“wrap_content” /> <com.videogo.widget.loading.LoadingView android:id=”@+id/remote_loading_iv" android:layout_width=“wrap_content” android:layout_height=“wrap_content” /> <com.videogo.widget.CheckTextButton android:id=“@+id/fullscreen_button” android:layout_width=“wrap_content” android:layout_height=“wrap_content” android:layout_alignParentRight=“true” android:layout_centerVertical=“true” android:layout_marginRight=“15dp” android:background=“@drawable/fullscreen_button_selector” /> <com.videogo.widget.loading.LoadingTextView android:id=“@+id/loadingTextView” android:layout_width=“match_parent” android:layout_height=“match_parent” android:layout_centerInParent=“true” android:background=“#efefef” android:visibility=“gone” /> <com.videogo.widget.PinnedHeaderListView android:id=“@+id/listViewandroid:layout_width=“match_parent” android:layout_height=“match_parent” android:layout_above=“@+id/delete_playback” android:cacheColorHint=“#00000000” android:divider=“@null” android:dividerHeight=“0dp” android:fadingEdge=“vertical” android:listSelector=“@null” android:overScrollFooter=“@null” android:scrollbarAlwaysDrawVerticalTrack=“false” android:scrollingCache=“false” /> <com.videogo.widget.PinnedHeaderListView android:id=“@+id/listView_sdkcloud” android:layout_width=“match_parent” android:layout_height=“match_parent” android:layout_above=“@+id/delete_playback_sdkcloud” android:cacheColorHint=“#00000000” android:divider=“@null” android:dividerHeight=“0dp” android:fadingEdge=“vertical” android:listSelector=“@null” android:overScrollFooter=“@null” android:scrollbarAlwaysDrawVerticalTrack=“false” android:scrollingCache=“false” /> <com.videogo.widget.PinnedHeaderListView android:id=“@+id/listView_device” android:layout_width=“match_parent” android:layout_height=“match_parent” android:layout_above=“@+id/delete_playback_device” android:cacheColorHint=“#00000000” android:divider=“@null” android:dividerHeight=“0dp” android:fadingEdge=“vertical” android:listSelector=“@null” android:overScrollFooter=“@null” android:scrollbarAlwaysDrawVerticalTrack=“false” android:scrollingCache=“false” /> 在上述代码顶部标题栏位置添加一个居中的年月日日期显示模块并且在显示年月日旁边添加一个图片按钮路径为drawable目录中playback_more_down1.png,这个年月份日期模块默认显示当天的年月日并且点击旁边新添加的图片按钮后显示一个年月日的日期选择器,用户选择日期选择器的年月日更新显示这个年月日日期显示模块。
最新发布
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值