自定义View组合模式

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:

<RelativeLayout xmlns:android="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">

    <com.itant.customedview.ImageText
        android: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"/>

</RelativeLayout>

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传送

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ithouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值