Andorid之使用ImageView和Selector配置文件制作多样式按钮(默认、点击、灰掉)

本文介绍如何在地图应用中实现缩放按钮的状态控制。通过配置ImageView和样式控制文件,可以实现在不同状态下(默认、按下、禁用)显示不同的图标资源。代码还展示了如何根据地图是否可缩放来动态启用或禁用缩放按钮。

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

 在做地图的时候  有个需求:

     当可以缩放地图的时候,放大按钮可以使用。当不可以使用地图的时候,放大按钮不可以使用。放大按钮在默认状态下是一个背景,点击的状态下是另一个背景。

     废话少说,代码奉上:

    一、ImageView配置文件片段。

        

<ImageView

     android:id="@+id/ivZoomOut"

     android:layout_height="wrap_content"

     android:layout_width="wrap_content"

     android:layout_marginRight="10px"

     android:layout_marginBottom="10px"

     android:src="@drawable/zoom_out_2"

     />

 二、样式控制配置文件zoom_out_2.xml:

  

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">    

<!-- Disabled -->

    <item android:state_enabled="false"

          android:state_pressed="false"

          android:textColor="@color/red"

          android:drawable="@drawable/zoom_out_disable" />    

<!-- Pressed -->

    <item android:state_enabled="true"

          android:state_pressed="true"

          android:textColor="@android:color/white"

          android:drawable="@drawable/zoom_out_pressed" />    

<!-- Focused -->

<!--     <item android:state_enabled="true"

          android:state_focused="true"

          android:textColor="@android:color/white"

          android:drawable="@drawable/list_item_focused" /> -->    

<!-- Default -->

    <item android:state_enabled="true"

          android:state_pressed="false"

          android:drawable="@drawable/zoom_out_default"/>

</selector>

 三、代码控制:

  

if(**){

          zoomOutView.setEnabled(false);

    }else{

         zoomOutView.setEnabled(true);

    }

转载于:https://my.oschina.net/u/1177694/blog/1505831

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值