android自定义View——组合控件

一,view_setting_item.xml组合控件布局文件:这里是组合控件中的元素,一个TextView,一个EditText.


<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/et_setting_item"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@android:color/transparent"//设置输入框背景透明
        android:gravity="center"/>
    <TextView
        android:id="@+id/tv_setting_item"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:gravity="center_vertical"
        android:layout_marginLeft="10dp"/>

FrameLayout>

二,attrs.xml文件:定义组合控件的自定义属性


<resources>
    <declare-styleable name="SettingItemView">
        <attr name="title_text" format="string"/>//TextView要显示的字符串
        <attr name="title_color" format="color"/>//TextView文字颜色
        <attr name="title_size" format="dimension"/>//TextView字符大小
        <attr name="title_hide" format="boolean"/>//TextView是否隐藏
        <attr name="content_text" format="string"/>//EditText要显示的字符串
        <attr name="content_size" format="dimension"/>//EditText字符大小
        <attr name="content_color" format="color"/>//EditText文字颜色
        <attr name="content_hint_color" format="color"/>//EditText提示文字颜色
        <attr name="content_focus" format="boolean"/>//EditText是否可以编辑
    declare-styleable>
resources>

三,SettingItemView.java组合控件类

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.sdlj.vehiclerepair.activity.R;

/**
 * Created by Chunna.zheng on 2017/9/9.
 */
public class SettingItemView extends FrameLayout{
    private EditText etMsg;
    private TextView tvTitle;

    private static final int DEFAULT_TEXT_COLOR = Color.BLACK;
    private static final int DEFAULT_EDIT_COLOR = Color.TRANSPARENT;
    private String titleText;
    private float titleSize;
    private int titleColor;
    private boolean isTitleHide;
    private float contentSize;
    private int contentColor;
    private int contentHintColor;
    private String contentText;
    private boolean isContentFocus;

    private int DEFAULT_TITLE_SIZE = 16;
    private int DEFAULT_TITLE_COLOR = Color.GRAY;
    private boolean DEFAULT_TITLE_HIDE = false;
    private int DEFAULT_CONTENT_SIZE = 16;
    private int DEFAULT_CONTENT_COLOR = Color.BLACK;
    private int DEFAULT_CONTENT_HINT_COLOR = Color.GRAY;
    private boolean DEFAULT_CONTENT_FOCUS = true;

    public SettingItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //加载属性值
        initAttrs(context, attrs);
        //加载布局
        initView(context);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.SettingItemView);
        titleText = typedArray.getString(R.styleable.SettingItemView_title_text);
        titleSize =  typedArray.getDimension(R.styleable.SettingItemView_title_size, DEFAULT_TITLE_SIZE);
        titleColor = typedArray.getColor(R.styleable.SettingItemView_title_color, DEFAULT_TITLE_COLOR);
        isTitleHide = typedArray.getBoolean(R.styleable.SettingItemView_title_hide, DEFAULT_TITLE_HIDE);
        contentText = typedArray.getString(R.styleable.SettingItemView_content_text);
        contentSize =  typedArray.getDimension(R.styleable.SettingItemView_content_size, DEFAULT_CONTENT_SIZE);
        contentColor = typedArray.getColor(R.styleable.SettingItemView_content_color, DEFAULT_CONTENT_COLOR);
        contentHintColor = typedArray.getColor(R.styleable.SettingItemView_content_hint_color, DEFAULT_CONTENT_HINT_COLOR);
        isContentFocus = typedArray.getBoolean(R.styleable.SettingItemView_content_focus, DEFAULT_CONTENT_FOCUS);
        typedArray.recycle();
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_setting_item, this);// 加载布局
        etMsg = (EditText) findViewById(R.id.et_setting_item);// 获取控件
        tvTitle = (TextView) findViewById(R.id.tv_setting_item);

        tvTitle.setText(titleText);
        tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,titleSize);
        //如果不设置这个文字会变得非常大
        tvTitle.getPaint().setTextSize(titleSize);
        tvTitle.setTextColor(titleColor);
        if(isTitleHide){
            tvTitle.setVisibility(GONE);
        }

        etMsg.setText(contentText);
        etMsg.setTextSize(TypedValue.COMPLEX_UNIT_SP,contentSize);
        //如果不设置这个文字会变得非常大
        etMsg.getPaint().setTextSize(contentSize);
        etMsg.setTextColor(contentColor);
        etMsg.setHintTextColor(contentHintColor);
        if(!isContentFocus){
            etMsg.setFocusable(false);
        }

    }

    //设置EditText值
    public void setContentText(String str){
        etMsg.setText(str);
    }

    //获取EditText值
    public String getContentText(){
        return etMsg.getText().toString().trim();
    }

}

四,组合控件使用


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"//自定义属性使用声明,必须要写
    tools:context="com.sdlj.vehiclerepair.activity.SettiingActivity">
    <com.sdlj.vehiclerepair.view.SettingItemView
        android:layout_width="match_parent"
        android:layout_height="48dp"
        app:title_text="头像"
        app:title_color = "#000000"
        app:title_size = "16sp"
        app:content_size = "16sp"
        app:content_focus="false"/>
LinearLayout>

你可能感兴趣的:(android-view)