自定义ItemToggleView

640?wx_fmt=gif

640?wx_fmt=gif

640?wx_fmt=other

极力推荐文章:欢迎收藏Android 干货分享 

640?wx_fmt=other

本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:

  1. 自定义View类实现

  2. 自定义View标签

  3. 自定义View 布局

  4. 自定义View 选择器

  5. 自定义View 素材

  6. Activity 自定义view布局引用

  7. Activity使用自定义View

自定义ItemToggleView 常用于Settings中,主要控制开关的开启与关闭。

自定义ItemToggleView实现效果如下:

640?wx_fmt=jpeg

开启.jpg

640?wx_fmt=other

关闭.jpg

1. 自定义View类实现

public class ItemToggleView extends RelativeLayout {
    private static final String TAG = "ItemToggleView";
    private TextView tv_title;
    private TextView tv_des;
    private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.programandroid";
    private String mDesTitle;
    private String mDesOff;
    private String mDesOn;
    private ImageView mImageView;
    private boolean isOnOFF;

    public ItemToggleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initAttrs(attrs);
        initUI(context);
    }

    public ItemToggleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(attrs);
        initUI(context);
    }

    public ItemToggleView(Context context) {
        super(context);
        initUI(context);
    }

    // 单独抽取出来的 xml--->view
    private void initUI(Context context) {
        View.inflate(context, R.layout.item_toggle_view, this);

        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_des = (TextView) findViewById(R.id.tv_des);

        mImageView = (ImageView) findViewById(R.id.switch_imageview);

        tv_title.setText(mDesTitle);
    }

    public boolean getCheck() {
        return isOnOFF;
    }

    /**
     * @param isCheck
     *            传递一个选中未选中的状态变量(true 选中 false未选中)
     */
    public void setCheck(boolean isCheck) {
        // mSwitchControlView.setChecked(isCheck);
        if (isCheck) {
            tv_des.setText(mDesOn);
            mImageView.setImageDrawable(getResources().getDrawable(
                    R.drawable.toggle_on));

        } else {
            tv_des.setText(mDesOff);
            mImageView.setImageDrawable(getResources().getDrawable(
                    R.drawable.toggle_off));
        }
        isOnOFF = isCheck;
    }

    /**
     * @param attrs
     *            包含了属性名称和属性值的set集合
     */
    private void initAttrs(AttributeSet attrs) {
        // 打印属性总个数
        /*
         * Log.i(tag, "attrs.getAttributeCount() = "+attrs.getAttributeCount());
         * for(int i=0;i<attrs.getAttributeCount();i++){ //获取所有的属性名称 Log.i(tag,
         * "属性名称 = "+attrs.getAttributeName(i)); Log.i(tag,
         * "属性值 = "+attrs.getAttributeValue(i)); }
         */

        mDesTitle = attrs.getAttributeValue(NAMESPACE, "desTitle");
        mDesOff = attrs.getAttributeValue(NAMESPACE, "desOff");
        mDesOn = attrs.getAttributeValue(NAMESPACE, "desOn");

        Log.i(TAG, mDesTitle);
        Log.i(TAG, mDesOff);
        Log.i(TAG, mDesOn);
    }
}

2. 自定义View标签

1.注意 :自定义 Android 命名空间

Android命名空间(xmlns:android="http://schemas.android.com/apk/res/android" )方法一样,想使用自定义view的属性,必须声明自定义view的命名空间(xmlns:programandroid="http://schemas.android.com/apk/res/com.programandroid")

2. 注意:自定义View 属性

自定义View 属性如下:

  programandroid:desOff="  不选中"
  programandroid:desOn="  选中"
  programandroid:desTitle=" WIFI " 

属性声明在res/values/attrs.xml中定义

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ItemCheckView"><!--SettingItemView申明控件,内部可以包含自定义属性-->
        <attr name="desTitle" format="string"/><!--标题描述属性-->
        <attr name="desOff" format="string"/><!--关闭更新属性-->
        <attr name="desOn" format="string"/><!--开启更新属性-->
    </declare-styleable>
</resources>

3. 自定义View 布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp" >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/primary_text_light"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tv_des"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_title"
        android:textColor="@android:color/secondary_text_light"
        android:textSize="14sp" />

    <ImageView
        android:id="@+id/switch_imageview"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/toggle_on" />

</RelativeLayout>

4. 自定义View 选择器

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

    <!-- 按下去的背景颜色显示效果 -->
    <item android:drawable="@drawable/list_item_bg_light_pressed" android:state_pressed="true"/>
    <!-- 获取焦点时背景颜色显示效果 -->
    <item android:drawable="@drawable/list_item_bg_light_pressed" android:state_focused="true"/>
    <!-- 没有任何状态下的背景颜色 -->
    <item android:drawable="@drawable/list_item_bg_light_normal"/>

</selector>

5. 自定义View 素材

640?wx_fmt=other

toggle_off.png

640?wx_fmt=other

toggle_on.png

6. Activity 自定义view布局引用

    <com.programandroid.CustomView.ItemToggleView
        xmlns:programandroid="http://schemas.android.com/apk/res/com.programandroid"
        android:id="@+id/custom_item_toggle_view"
        android:layout_width="wrap_content"
        android:layout_height="55dp"
        android:background="@drawable/listview_item_selector"
        programandroid:desOff="  关闭"
        programandroid:desOn="  开启"
        programandroid:desTitle=" WIFI " />

7. Activity使用自定义View

    /**
     * 自定义 ItemToggleView
     */
    private void InitItemToggleView() {
        // TODO Auto-generated method stub
        final ItemToggleView mItemToggleView = (ItemToggleView) findViewById(R.id.custom_item_toggle_view);
        mItemToggleView.setCheck(false);
        mItemToggleView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mItemToggleView.setCheck(!mItemToggleView.getCheck());
            }
        });
    }

640?wx_fmt=jpeg

640?wx_fmt=jpeg

长按识别二维码,领福利

至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

如有侵权,请联系小编,小编对此深感抱歉,届时小编会删除文章,立即停止侵权行为,请您多多包涵。

640?wx_fmt=gif

640?wx_fmt=png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员Android

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值