自定义控件属性

自定义控件里面没有属性,没有文字,没有图片,所以会再写一个布局(有Textview,Imageview),用自定义控件的属性去设置你的那个布局里面的属性值;

1.新建一个自定义控件的类(ContentTitle),继承自LinearLayout(布局),实现方法;

2.初始化布局控件(R.layout.contentTitle),写一个TextView,ImageView;

3.在values中新建一个attrs.xml自定义其属性值;

4.在主xml中写自定义控件,设置其属性;

5.在类里面得到布局,以及控件,属性,设置其属性;

6.展示;

//第一步:

public class ContentTitle extends LinearLayout {

    private LinearLayout rootView;
    private TextView name;
    private ImageView image;

    public ContentTitle(Context context) {
        this(context, null);
    }

    public ContentTitle(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public ContentTitle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //初始化控件
        initView(context);
        initAttrs(context, attrs);
    }

//第二步:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher" />
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义属性"/>

</LinearLayout>

//第三步:values--->attrs.xml

<resources>
    <declare-styleable name="ContentTitle">
        <attr name="left_image" format="reference"></attr>
        <attr name="content_text" format="string"></attr>
        <attr name="content_text_color" format="color"></attr>
        <attr name="content_text_size" format="dimension"></attr>

    </declare-styleable>

</resources>

//第四步:

<com.bw.a123.high5_day.ContentTitle
    android:id="@+id/contentTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    customer:content_text="1111111111111"
    customer:content_text_color="#ae2c2c"
    customer:content_text_size="20dp"
    customer:left_image="@mipmap/q9">

</com.bw.a123.high5_day.ContentTitle>

//第五步:

//===========================初始化控件====================================================
private void initView(Context context) {
    rootView = (LinearLayout) View.inflate(context, R.layout.item, this);
    name = rootView.findViewById(R.id.name);
    image = rootView.findViewById(R.id.image);
}

//===========================初始化属性====================================================
private void initAttrs(Context context, AttributeSet attrs) {

    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ContentTitle);
    //设置图片
    Drawable drawable = typedArray.getDrawable(R.styleable.ContentTitle_left_image);
    //判断是否为空
    if (drawable != null) {
        image.setImageDrawable(drawable);
    }
    //设置文字
    CharSequence text = typedArray.getText(R.styleable.ContentTitle_content_text);
    if (!TextUtils.isEmpty(text)) {
        name.setText(text);
    }
    //设置颜色
    int color = typedArray.getColor(R.styleable.ContentTitle_content_text_color, -1);
    if (color != -1) {
        name.setTextColor(color);
    }
    //设置大小
    float dimension = typedArray.getDimension(R.styleable.ContentTitle_content_text_size, 0f);
    name.setTextSize(dimension);

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值