attrs.xml:

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="ImageText">        <attr name="src" format="reference|color" />        <attr name="imageWidth" format="dimension" />        <attr name="imageHeight" format="dimension" />        <attr name="textSize" format="dimension" />        <attr name="text" format="string" />    declare-styleable>resources>

组合控件的布局view_image_text.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="vertical"    android:gravity="center_horizontal" >    <ImageView        android:id="@+id/iv_image"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/tv_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="@drawable/selector_student_text" />LinearLayout>

activity_main.xml:

"http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    xmlns:it="http://schemas.android.com/apk/res-auto">    id="@+id/it_test"        it:src="@mipmap/ic_launcher"        it:imageWidth="20dp"        it:imageHeight="20dp"        it:text="hello"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>

drawable

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:color="@android:color/holo_blue_dark" android:state_selected="true"/>    <item android:color="@android:color/holo_blue_dark" android:state_checked="true"/>    <item android:color="@android:color/holo_blue_dark" android:state_pressed="true"/>    <item android:color="@android:color/darker_gray"/>selector>

MainActivity.java

package com.itant.customedview;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {    private ImageText it_test;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        it_test = (ImageText) findViewById(R.id.it_test);        it_test.setOnClickListener(this);    }    @Override    public void onClick(View v) {        // TODO Auto-generated method stub        Toast.makeText(this, "click", Toast.LENGTH_SHORT).show();    }}

自定义控件:

package com.itant.customedview;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.view.LayoutInflater;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;public class ImageText extends LinearLayout {    private TextView textView;    public ImageText(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub        LayoutInflater.from(context).inflate(R.layout.view_image_text, this);        ImageView imageView = (ImageView) findViewById(R.id.iv_image);        textView = (TextView) findViewById(R.id.tv_text);        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ImageText);        int imageResource = typedArray.getResourceId(R.styleable.ImageText_src, 0);        int imageWidth = UIUtils.dip2px(context, typedArray.getDimension(R.styleable.ImageText_imageWidth, 40));        int imageHeight = UIUtils.dip2px(context, typedArray.getDimension(R.styleable.ImageText_imageHeight, 40));        float textSize = typedArray.getDimension(R.styleable.ImageText_textSize, 12);        String text = typedArray.getString(R.styleable.ImageText_text);        typedArray.recycle();        imageView.setBackgroundResource(imageResource);        LayoutParams params = new LayoutParams(imageWidth, imageHeight);        imageView.setLayoutParams(params);        textView.setTextSize(textSize);        textView.setText(text);    }    /*    @Override    public boolean onTouchEvent(MotionEvent event) {        // TODO Auto-generated method stub        switch (event.getAction()) {        case MotionEvent.ACTION_DOWN:            textView.setTextColor(getResources().getColor(R.color.red));            break;        case MotionEvent.ACTION_UP:            textView.setTextColor(getResources().getColor(R.color.gray));            break;        default:            break;        }        //performClick();        return super.onTouchEvent(event);    }*/}

github传送

更多相关文章

  1. [控件]SeekBar拖动条
  2. Android(安卓)UI控件-Spinner(下拉列表)
  3. android text 中英文混排 换行的问题
  4. 去除Dialog边框
  5. Android(安卓)控件布局常用属性
  6. 安卓倒计时
  7. [Android]Fragment点击穿透问题
  8. 弹出式菜单PopMenu
  9. Android(安卓)在自定义view中动态设置布局规则

随机推荐

  1. Android中SDK供第三方调用实现详解
  2. android 记录所有打开的Activity,退出程序
  3. ANDROID Porting系列八、Keymaps and Key
  4. android webview goback 跳过页面302自动
  5. Android底部虚拟按键遮挡应用
  6. Android绘图之SweepGradient(10)
  7. 给View设置独立border
  8. Android中AutoCompleteTextView控件的使
  9. Android 判断动画结束
  10. Android绘制三角形实现带小三角的PopupWi