Android 自定义控件

为什么使用自定义控件?

一般像我们初学者,在做界面的过程中输入框(EditView)是经常用到的。为了用户填写信息区别开来我们往往会在EditView前面加个文本框(TextView)类似的输入(姓名:、年龄:…)。那么问题来了如果要输入的信息有7到8条呢,是不是很麻烦,这其中还会涉及到布局问题。所以我分享一种更方便的方法让他们两个控件变成一个怎么样。

my_edit_view.xml
// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <TextView
        android:textSize="20sp"
        android:gravity="center"
        android:text="name"
        android:id="@+id/tv_name"
        android:layout_width="80dp"
        android:layout_height="40dp" />
    <EditText
        android:layout_marginLeft="80dp"
        android:hint="please enter"
        android:id="@+id/et_conter"
        android:layout_width="match_parent"
        android:layout_height="40dp" />
</merge>

这是一个简单的布局
效果图如下:
在这里插入图片描述
接下来再看看我们的自定义控件类

MyEditText .class
package com.example.syt98.myappcustomview;

/**
 * Created by syt98 on 2018/12/10.
 */
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MyEditText extends RelativeLayout {
    private TextView tv_name;
    private EditText et_conter;
    private String tname;
    private int etback;
    public MyEditText(Context context) {
        super(context);
        init();
    }

    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyEditText);
        // 解析集合中的属性tvName属性
        // 该属性的id为:R.styleable.MyEditText_tvName
        // 将解析的属性传入到变量当中
        tname = a.getText(R.styleable.MyEditText_tvName).toString().trim();
        etback = a.getColor(R.styleable.MyEditText_etBack,Color.WHITE);
        // 解析后释放资源
        a.recycle();
        init();
    }

    public MyEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        inflate(getContext(), R.layout.my_edit_view, this);
        this.tv_name = (TextView)findViewById(R.id.tv_name);
        this.et_conter = (EditText) findViewById(R.id.et_conter);
        tv_name.setText(tname);
        et_conter.setBackgroundColor(etback);
    }
}

在写这个类之前呢,我们应该把attr.xml中的自定义属性和main_activity.xim中控件写好。

attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyEditText">
        <attr name="tvName" format="string"/>
        <attr name="etBack" format="reference|color"/>
    </declare-styleable>
</resources>

attr.xml需放在values下面,
其中的format代表的是自定义属性的值类型,像我想设置输入框背景(etBack),那么它的属性值就有颜色或者导入用"reference|color"表示。

main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.syt98.myappcustomview.MainActivity">
    <com.example.syt98.myappcustomview.MyEditText
        app:etBack="#bbf4ff"
        app:tvName="套你猴子"
        android:layout_width="match_parent"
        android:layout_height="40dp">
    </com.example.syt98.myappcustomview.MyEditText>
</LinearLayout>

在自定义控件中的自定义属性都是app哦。
运行效果图:
在这里插入图片描述

总结:

整体还是好理解的,这只是其中一种非常简单的自定义控件,上面提到的format不太清楚的,可以百度了解一下哦。有什么问题都可以私聊小白我。0.0
QQ:2714730493
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值