Android View之组合控件实例(一)

Android View之组合控件实例(一)_第1张图片

package com.xiong.demo1;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

/**
 * Created by xionglh on 2015/12/24.
 */
public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        TitleBarView titleBarView = (TitleBarView) findViewById(R.id.tbar_test);
        titleBarView.getTextViewRigth().setVisibility(View.GONE);
        titleBarView.setTitleBarChangerLiseter(new ItitleOnChangeLister() {
            @Override
            public void setLeftOnClickLister() {
                finish();
            }

            @Override
            public void setRigthOnClickLister() {

            }
        });
    }

}


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

    <com.xiong.demo1.TitleBarView
        android:id="@+id/tbar_test"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        xionglh:titleBar_center_text="首页"
        xionglh:titleBar_center_textColor="@android:color/black"
        xionglh:titleBar_center_text_size="18sp"
        xionglh:titleBar_left_bg="@mipmap/left_back"
        xionglh:titleBar_right_text="安全中心"
        xionglh:titleBar_right_text_size="12sp"/>
</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TitleBar">

        <attr name="titleBar_center_text_size" format="dimension"/>
        <attr name="titleBar_center_text" format="string"/>
        <attr name="titleBar_center_textColor" format="color"/>

        <attr name="titleBar_left_bg" format="reference"/>

        <attr name="titleBar_right_text_size" format="dimension"/>
        <attr name="titleBar_right_text" format="string"/>
        <attr name="titleBar_right_textColor" format="color"/>

    </declare-styleable>

</resources>


package com.xiong.demo1;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by xionglh on 2016/1/4.
 */
public class TitleBarView extends RelativeLayout {

    private ItitleOnChangeLister mItitleOnChangeLister;

    private ImageView mImgLeft;
    private TextView mTxtCenter, mTxtRigth;

    private float mTitleCenterTextSize;//标题字体大小
    private String mTitleCenterText;//标题文字
    private int mTitleCenterTextColor;//标题颜色

    private int mLeftBg;//左边返回按钮

    private float mTitleRigthTextSize;//标题字体大小
    private String mTitleRigthText;//标题文字
    private int mTitleRigthColor;//标题颜色

    public TitleBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        int defualtSize = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics());
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.TitleBar);
        mTitleCenterTextSize = typedArray.getDimension(R.styleable.TitleBar_titleBar_center_text_size, defualtSize);
        mTitleCenterText = typedArray.getString(R.styleable.TitleBar_titleBar_center_text);
        mTitleCenterTextColor = typedArray.getColor(R.styleable.TitleBar_titleBar_center_textColor, Color.RED);
        mLeftBg = typedArray.getResourceId(R.styleable.TitleBar_titleBar_left_bg, R.mipmap.left_back);
        mTitleRigthTextSize = typedArray.getDimension(R.styleable.TitleBar_titleBar_right_text_size, defualtSize);
        mTitleRigthText = typedArray.getString(R.styleable.TitleBar_titleBar_right_text);
        mTitleRigthColor = typedArray.getColor(R.styleable.TitleBar_titleBar_right_textColor, Color.RED);
        typedArray.recycle();
        initView();
    }

    private void initView() {
        mTxtCenter = new TextView(getContext());
        mTxtCenter.setText(mTitleCenterText);
        mTxtCenter.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleCenterTextSize);
        mTxtCenter.setTextColor(mTitleCenterTextColor);
        LayoutParams centerParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        centerParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
        addView(mTxtCenter, centerParams);
        mTxtRigth = new TextView(getContext());
        mTxtRigth.setText(mTitleRigthText);
        mTxtRigth.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleRigthTextSize);
        mTxtRigth.setTextColor(mTitleRigthColor);
        LayoutParams rigthParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rigthParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
        rigthParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE);

        addView(mTxtRigth, rigthParams);
        mImgLeft = new ImageView(getContext());
        mImgLeft.setImageResource(mLeftBg);
        LayoutParams leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        leftParams.setMargins(6, 0, 0, 0);
        leftParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE);
        addView(mImgLeft, leftParams);
        View view = new View(getContext());
        view.setMinimumWidth(1);
        view.setBackgroundColor(getResources().getColor(R.color.gray_767676));
        LayoutParams viewParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 1);
        viewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        addView(view, viewParams);
        mImgLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mItitleOnChangeLister.setLeftOnClickLister();
            }
        });

        mTxtRigth.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mItitleOnChangeLister.setRigthOnClickLister();
            }
        });

    }

    public void setTitleBarChangerLiseter(ItitleOnChangeLister ititleOnChangeLister) {
        this.mItitleOnChangeLister = ititleOnChangeLister;
    }


    public ImageView getLeftImage() {
        return mImgLeft;
    }

    public TextView getTextViewCenter() {
        return mTxtCenter;
    }

    public TextView getTextViewRigth() {
        return mTxtRigth;
    }
}


package com.xiong.demo1;

/**
 * Created by xionglh on 2016/1/4.
 */
public interface ItitleOnChangeLister {


    void setLeftOnClickLister();
    void setRigthOnClickLister();

}


你可能感兴趣的:(android,实例,控件,android自定义控件)