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等的赋值即可

更多相关文章

  1. WebView控件中的javascript与Android本地功能交互
  2. 常见UI控件的使用-富文本、textview
  3. Android——ImageButton【图片按钮】的点击事件与属性
  4. 关于LinearLayout布局中,子控件平分宽度
  5. TextView设置一行最多显示6个字是什么属性?
  6. 【Android 开发】:UI控件之拖动条控件 SeekBar的使用方法
  7. android多框架实现短视频应用、3D手势旋转、banner控件、指南针

随机推荐

  1. SQL分页查询存储过程代码分享
  2. MySQL5.7 group by新特性报错1055的解决
  3. CentOs7.x安装Mysql的详细教程
  4. Mac下忘记mysql密码重新设置密码的图文教
  5. mysql limit 分页的用法及注意要点
  6. mysql limit分页优化详细介绍
  7. MySQL数据库 1067错误号的解决方法
  8. MySQL数据库基础命令大全(收藏)
  9. centos7安装mysql并jdbc测试教程
  10. 理解MySQL——索引与优化总结