自定义View案例之自定义组合控件

一、概念

自定义组合控件就是多个控件组合起来成为一个新的控件

二、案例

实现自定义导航栏

1、源码

编写布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorAccent">

    <ImageView
        android:id="@+id/header_left_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="@drawable/ic_arrow_back_black_24dp"/>

    <TextView
        android:id="@+id/header_center_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <ImageView
        android:id="@+id/header_right_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/ic_cancel_small_dark"
        android:layout_centerVertical="true"/>
</RelativeLayout>

设置自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TitleView">
        <attr name="text_title" format="string"/>
    </declare-styleable>
</resources>

继承RelativeLayout,实现构造方法
设置自定义属性以及对外提供的监听方法

public class TitleView extends RelativeLayout {
    private ImageView img_left;
    private TextView text_center;
    private ImageView img_right;

    public TitleView(Context context) {
        super(context);
        initView(context);
    }

    public TitleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
		//自定义属性设置title
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleView);
        String text_title = typedArray.getString(R.styleable.TitleView_text_title);
        text_center.setText(text_title);
    }

    public TitleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.title_layout, this,true);
        img_left = findViewById(R.id.header_left_img);
        text_center = findViewById(R.id.header_center_text);
        img_right = findViewById(R.id.header_right_img);
    }

	//设置对外提供方法
    public void setLeftListener(OnClickListener listener) {
        img_left.setOnClickListener(listener);
    }

	//设置对外提供方法
    public void setRightListener(OnClickListener listener) {
        img_right.setOnClickListener(listener);
    }
}

引用自定义控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.android.myview.TitleView
        android:id="@+id/title"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:text_title="this is title"/>

</LinearLayout>

在主界面调用自定义View,并设置左右两边的点击事件

public class MainActivity extends AppCompatActivity{
    private TitleView titleView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        titleView = findViewById(R.id.title);
        titleView.setLeftListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "left", 1000).show();
            }
        });

        titleView.setRightListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "right", 1000).show();
            }
        });
    }
}

2、效果图

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值