学习笔记-----listview中含有togglebutton

还是小菜鸟一直,今天开始尝试写博客,其实就是自己的学习笔记吧
今天要写的是一个listview中含有togglebutton的一些自己所需要注意的地方。
其余的就不说,就直接标题,缘由是要写一个隐私设置,需要一个开关,之前用preference来写,但是自己还是小菜鸟,preference用不好,所以又换成普通activity来写,在preference时候的xml是有一个switchPreference的,有他的title,summay等,很标准的,但是有个问题,版本不同他的样式是不同的,要求样式一致就要自己设开关的样式,我就用的togglebutton 来弄
太多废话了。。

因为这个togglebutton如果用系统自己的,会因为版本不同样式会不一样,所以先找好自己的toggle样式图片,我用的是
这里写图片描述
以及
这里写图片描述

分别是开和关的样式,大概样子就是这样

然后把他们分别匹配到togglebutton上

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
        android:drawable="@drawable/bitmap_privacy_switch_on" /> <!-- pressed -->
    <item android:state_checked="false"
        android:drawable="@drawable/bitmap_privacy_switch_off"/> <!-- default/unchecked -->
</selector>

我要做的是在listview里的toggle,所以做好toggle所在的item布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content"  android:minHeight="50dp"
    android:clickable="true"
    android:descendantFocusability="blocksDescendants" >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#5C5C5C"
        android:paddingTop="20dp"
        android:paddingLeft="15dp"
        android:id="@+id/title_tv"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="15dp"
        android:textColor="#C8C8C8"
        android:id="@+id/describe_tv"
        />
    </LinearLayout>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="22dp"
        android:layout_alignParentRight="true" >
    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn=""
        android:textOff=""
        android:background="@drawable/toggle_privacy_set"
        android:layout_alignParentRight="true"
        android:id="@+id/privacy_set_tb"
        android:focusable="false"
        android:clickable="true"
        />
    </RelativeLayout>
    </RelativeLayout>

代码习惯什么的小细节就不要在意了,

要注意的几点是
1,因为所用的togglebutton用我们之前写好的两个bitmap来作为我们想要的样式,用的是background,但是如果直接这样的话,会导致你的图片被拉伸成系统本来的togglebutton大小轮廓,超级难看,所以为了不让它拉伸,需要再加个

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/privacy_switch_on_bm"
    android:src="@mipmap/ic_switch_on"
    android:tileMode="disabled" android:gravity="top">
</bitmap>  

这样图片作为background时就不会被拉伸,当然两张图都要这样弄
在将这两张图设置成togglebutton的开关样式,就是刚开始所提到的

2,
item的xml问价中根目录的

android:clickable="true"
android:descendantFocusability="blocksDescendants"

以及togglebutton中

android:focusable="false"
android:clickable="true"

这几句是为了不让item和togglebutton点击事件冲突,这四句加上是最保险的,让item和togglebutton各自的点击事件都响应

差不多就是这样了

继续加油!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值