zoukankan      html  css  js  c++  java
  • android自定义控件

    1.自定义组件(按钮)xml文件如下

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/id_paste_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/bottom_item_selector"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingBottom="4dp"
        android:paddingTop="4dp" >
    
        <ImageView
            android:id="@+id/btn_icon"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:src="@drawable/paste" />
    
        <TextView
            android:id="@+id/btn_txt"
            style="@style/myTextApprearence.micro.white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tab_paste" />
    
    </LinearLayout>

    2.这个按钮有两个属性是变的,一个是图片,一个是文字,在res/values/attrs.xml文件自定义两个属性:text, icon

     

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="BottomBarButton" >
            <attr name="android:text" type="text" />
            <attr name="android:icon" type="drawable" />
            <!--自定义的属性名-->
            <attr name="icon_unclick" format="integer" /> 
            <attr name="underline_color" format="reference|color"/>
            <attr name="show_underline" format="boolean/>
           
        </declare-styleable>
    </resources>    

     

     

    java代码对这个自定义控件进行赋值

    public class BottomBarBtn extends LinearLayout{
    
        public BottomBarBtn(Context context, AttributeSet attrs) {
            super(context, attrs);
            LayoutInflater.from(context).inflate(R.layout.bottom_bar_button, this, true);
            
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BottomBarButton);
            //获取自定义属性'text'的值
            CharSequence iconDesc = ta.getText(R.styleable.BottomBarButton_android_text);
            //获取自定义属性'icon'的值    
            Drawable icon = ta.getDrawable(R.styleable.BottomBarButton_android_icon);
            //获取自定义属性'icon_unclick'的值 
        Drawable disIcon = ta.getDrawable(R.styleable.BottomBarButton_icon_unclick);
            TextView btnText = (TextView) findViewById(R.id.btn_txt);
            if(null != iconDesc){
                btnText.setText(iconDesc);
            }
            ImageView btnIcon = (ImageView) findViewById(R.id.btn_icon);
            if(null != icon){
                btnIcon.setImageDrawable(icon);
            }
            ta.recycle();
        }
    }

    3.布局文件中使用这个控件

    重点在这个定义:xmlns:my_name_space="http://schemas.android.com/apk/res/com.your.package"

    使用:my_name_space:icon_unclick="@drawable/paste_unclick"

    <LinearLayout xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:my_name_space="http://schemas.android.com/apk/res/com.your.package"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/top_bar_background"
        android:orientation="vertical" >
    <!--下面的两个属性是自定义的:icon, text-->
    <com.ui.customview.BottomBarBtn
                android:id="@+id/id_paste_button"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:icon="@drawable/paste"
                my_name_space:icon_unclick="@drawable/paste_unclick"
                android:text="@string/tab_paste" />

    4.如果是动态添加控件,要以直接在代码中new 这个对象,然后进行相应text等的赋值即可

  • 相关阅读:
    取消Git每次拉取、提交推送都要输入密码
    input输入框的oninput和onchange事件
    NPM学习笔记
    命令行语法格式中常用符号的含义
    npm卸载模块报错
    软件的Alpha、Beta、GM、OEM、LTS等版本的含义(转)
    MySQL常用指令
    Git常用指令
    .net Core 解决Form value count limit 1024 exceeded. (文件上传过大)
    EF Core 迁移整理
  • 原文地址:https://www.cnblogs.com/baron89/p/3627022.html
Copyright © 2011-2022 走看看