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. android属性
  2. 常见UI控件的使用-富文本、textview
  3. WebView控件中的javascript与Android本地功能交互
  4. Android——ImageButton【图片按钮】的点击事件与属性
  5. Android(安卓)面试题之基础(不断更新)
  6. TextView设置一行最多显示6个字是什么属性?
  7. 关于LinearLayout布局中,子控件平分宽度
  8. android 动态布局
  9. Error:Execution failed for task ':app:processDebugManifest'.

随机推荐

  1. Android(安卓)实用工具Hierarchy Viewer
  2. android 远程下载与pull解析技术
  3. Android之UI学习篇六:ImageView实现图片旋
  4. Android(安卓)开发札记初级(一)Android概述
  5. Android菜单详解
  6. 什么是Android(安卓)- 开放手机联盟和And
  7. android中的数据库操作
  8. Android实现滑动的7种方法
  9. Android如何检测网络的类型为3G、2G、wap
  10. Android视频采集