美文网首页
简单的自定义组合控件

简单的自定义组合控件

作者: 我是你森哥哥 | 来源:发表于2017-06-08 15:37 被阅读0次

效果展示图

捕获.PNG

1.继承 RelativeLayout 串联构造方法

public class CustomPhotoOpenView extends RelativeLayout {
    private ImageView iv1,iv2;
    private TextView tv;

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

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

    public CustomPhotoOpenView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initAttrs(context,attrs);
    }

2.初始化布局文件

private void initView() {
        View view = View.inflate(getContext(), R.layout.custom_item_photoopen, null);
        this.addView(view);

        iv1 = (ImageView) view.findViewById(R.id.iv_custom_photoopen_open);
        iv2 = (ImageView) view.findViewById(R.id.iv_custom_photoopen_photo);

        tv = (TextView) view.findViewById(R.id.tv_custom_photoopen_text);
    }

3. 强属性赋给控件

//把自定义属性设置给自定义控件,并没有什么意义,因为文本在显示的时候是在自定义控件的布局文件中的textview中显示的
//所以,为了能显示文本,需要在自定义控件中将自定义属性的值获取出来,设置给textview进行显示
//控件的所有的属性都保存在attrs中,所以可以在attrs中获取属性的值
//通过命名空间和属性的名称,获取属性的值

 private void initAttrs(Context context, AttributeSet attrs) {

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomPhotoOpenView);

        String title= ta.getString(R.styleable.CustomPhotoOpenView_pttext);
        int open = ta.getResourceId(R.styleable.CustomPhotoOpenView_ptopen, -1);
        int photo = ta.getResourceId(R.styleable.CustomPhotoOpenView_ptphoto, -1);

        tv.setText(title);
        iv1.setImageResource(open);
        iv2.setImageResource(photo);

        ta.recycle();

    }

布局文件如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mine_integral"
    android:layout_width="match_parent"
    android:layout_height="@dimen/y25">

    <ImageView
        android:id="@+id/iv_custom_photoopen_photo"
        android:layout_width="@dimen/x20"
        android:layout_height="@dimen/y20"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/x15"
        android:src="@mipmap/wodejfen" />

    <TextView
        style="@style/Mine_More"
        android:id="@+id/tv_custom_photoopen_text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/iv_custom_photoopen_photo"
        android:text="我的积分" />

    <ImageView
        android:id="@+id/iv_custom_photoopen_open"
        style="@style/style_qianwang" />
</RelativeLayout>

相关文章

  • Android入门06 -- 自定义控件

    自定义组合控件 将几个子控件组合在一起,形成一个可复用的新的组合控件,自定义组合控件一般继承自RelativeLa...

  • Android自定义控件:带动画效果的手机号输入框 (3-4-4

    项目中很多地方,使用到了自定义控件。 简单点的,如个性控件的定制,多个组件的组合封装等。我们需要了解自定义控件的基...

  • android2019-01-03

    1.View的绘制流程自定义控件:1、组合控件。这种自定义控件不需要我们自己绘制,而是使用原生控件组合成的新控件。...

  • 11-自定义组合控件以及使用

    一、自定义组合控件介绍 开发中,为了使用的方便,经常把一些控件组合成一个控件,那样就成为了我们的自定义组合控件,严...

  • Android组合控件详解 & 自定义属性

    组合控件详解 & 自定义属性 摘录来源:极客学院WiKi 组合控件是自定义控件的一种,只不过它是由其他几个原生控件...

  • 【Android】自定义控件

    Android自定义控件可以有三种实现方式 组合原生控件自己绘制控件继承原生控件 1 组合原生控件 1.1 组合原...

  • 组合控件2——海贼王选项菜单

    之前的自定义控件——初识自定义控件,我们了解到了自定义控件分为三种,自制控件,组合控件,拓展控件。而我们在自制控件...

  • 每天五道Android面试题,轻松进大厂2018-12-20

    一、View的绘制流程 自定义控件: 1、组合控件。这种自定义控件不需要我们自己绘制,而是使用原生控件组合成的新控...

  • 自定义组合控件,事件传递响应规则

    自定义组合控件 当系统提供的控件,不足以满足我们开发需求时,可以通过自定义控件来实现更好的效果。 组合控件的实现步...

  • 组合控件1—— 设置框

    之前的自定义控件——初识自定义控件,我们了解到了自定义控件分为三种,自制控件,组合控件,拓展控件。 而我们在自制控...

网友评论

      本文标题:简单的自定义组合控件

      本文链接:https://www.haomeiwen.com/subject/igctqxtx.html