zoukankan      html  css  js  c++  java
  • android项目自定义组合控件添加属性

    首先要在values文件下新建立一个文件arrts.xml,这个文件就是用来说明键名称是做什么的,和值的类型

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
         <declare-styleable name="SeetingView">
            <attr name="up" format="string" />
            <attr name="down_on" format="string" />
            <attr name="down_off" format="string" />
        </declare-styleable>
    </resources>

    然后在应用的布局里需要加上命名空间,第二行就是自己的命名空间,这个组成就是前面的xmlns:itheima名字然后是"http://schemas.android.com/apk/res/+包名

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:itheima="http://schemas.android.com/apk/res/com.itheima.superman"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/home_bg">
    
        <TextView 
            style="@style/TitleStyle"
            android:text="设置中心"
            />
        <com.itheima.view.SeetingView
            android:id="@+id/stv_updata"
            android:layout_marginTop="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            itheima:up="自动更新设置"
            itheima:down_on="自动更新已经开启"
             itheima:down_off="自动更新已经关闭"
            />
       
    
    </LinearLayout>

    最后我们就要在自定义控件的类里获取这些值了然后给他们设置上。

    package com.itheima.view;
    
    import com.itheima.superman.R;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.CheckBox;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    
    public class SeetingView extends RelativeLayout{
    
        private TextView tv_up;
        private TextView tv_down;
        private CheckBox ck_right;
        private String up;
        private String down_on;
        private String down_off;
        //命名空间
        private String nameSpace = "http://schemas.android.com/apk/res/com.itheima.superman";
        //自定义样式调用
        public SeetingView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            initView();
        }
        //自定义属性调用
        public SeetingView(Context context, AttributeSet attrs) {
            super(context, attrs);
            //获取命名空间下,键名称为“up”的值
            up = attrs.getAttributeValue(nameSpace, "up");
            down_on = attrs.getAttributeValue(nameSpace, "down_on");
            down_off = attrs.getAttributeValue(nameSpace, "down_off");
            initView();
        }
        //声明调用
        public SeetingView(Context context) {
            super(context);
            initView();
        }
        //初始化布局
        private void initView(){
            //给这个布局一个父控件
            View.inflate(getContext(),R.layout.item_seeting, this);
            tv_up = (TextView) findViewById(R.id.tv_up);
            tv_down = (TextView) findViewById(R.id.tv_down);
            ck_right = (CheckBox) findViewById(R.id.ck_right);
            //在初始化的时候设置这个值
            tv_up.setText(up);
        }
        //设置顶部文字
        public void setUp(String text){
            tv_up.setText(text);
        }
        //设置底部文字
        public void setDown(String text){
            tv_down.setText(text);
        }
        //是否被选中
        public boolean isChecked(){
            return ck_right.isChecked();
        }
        //设置选择状态
        public void setChecked(boolean b){
            if(b){
                ck_right.setChecked(b);
                tv_down.setText(down_on);
            }else{
                ck_right.setChecked(b);
                tv_down.setText(down_off);
            }
        }
        
    
    }
  • 相关阅读:
    HDU 5486 Difference of Clustering 图论
    HDU 5481 Desiderium 动态规划
    hdu 5480 Conturbatio 线段树 单点更新,区间查询最小值
    HDU 5478 Can you find it 随机化 数学
    HDU 5477 A Sweet Journey 水题
    HDU 5476 Explore Track of Point 数学平几
    HDU 5475 An easy problem 线段树
    ZOJ 3829 Known Notation 贪心
    ZOJ 3827 Information Entropy 水题
    zoj 3823 Excavator Contest 构造
  • 原文地址:https://www.cnblogs.com/84126858jmz/p/5001494.html
Copyright © 2011-2022 走看看