android自定义控件

项目中很多地方需要用到的一个标题,自己第一次封装一个控件

public class TopView extends LinearLayout implements OnClickListener {
    private TextView mTvTitle, mTvRight;
    private ImageView mImgRight, mImgLeft;
    private int mTvColor;
    private float mTvSize, mRightSize;
    private String mTvText, mRightText;
    private Drawable mImgLeftSrc, mImgRightSrc;
    private TopViewListener mTopViewLeftListener = null,
            mTopViewRightListener = null;
    private boolean isRightShow, isLeftShow;

    public TopView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.topView);
        mTvColor = typedArray.getColor(R.styleable.topView_TopViewColor,
                context.getResources().getColor(R.color.white));
        mTvSize = typedArray.getDimensionPixelSize(
                R.styleable.topView_TopViewSize, 20);
        mRightSize = typedArray.getDimensionPixelSize(
                R.styleable.topView_RightSize, 0);
        mTvText = typedArray.getString(R.styleable.topView_TopViewText);
        mRightText = typedArray.getString(R.styleable.topView_RightText);
        mImgLeftSrc = typedArray.getDrawable(R.styleable.topView_LeftSrc);
        mImgRightSrc = typedArray.getDrawable(R.styleable.topView_RightSrc);
        isRightShow = typedArray.getBoolean(R.styleable.topView_isRightShow,
                true);
        isLeftShow = typedArray
                .getBoolean(R.styleable.topView_isLeftShow, true);

        DisplayMetrics dm = getScreenParams(context);
        int densityDPI = dm.densityDpi;
        computeTextSize(densityDPI);

        LayoutInflater.from(context).inflate(R.layout.topview, this);

        mTvTitle = (TextView) this.findViewById(R.id.tv_title);
        mImgLeft = (ImageView) this.findViewById(R.id.img_left);
        mImgRight = (ImageView) this.findViewById(R.id.img_right);
        mTvRight = (TextView) this.findViewById(R.id.tv_right);
        LinearLayout layout = (LinearLayout) this.findViewById(R.id.layout);

        mTvTitle.setTextColor(mTvColor);
        mTvTitle.setTextSize(mTvSize);
        mTvTitle.setText(mTvText);
        mTvRight.setTextSize(mRightSize);
        mTvRight.setText(mRightText);
        mImgLeft.setImageDrawable(mImgLeftSrc);
        if (isLeftShow) {
            mImgLeft.setVisibility(View.VISIBLE);
        } else {
            mImgLeft.setVisibility(View.GONE);
        }
        mImgLeft.setOnClickListener(this);
        mImgRight.setImageDrawable(mImgRightSrc);
        layout.setOnClickListener(this);
        if (isRightShow) {
            layout.setVisibility(View.VISIBLE);
        } else {
            layout.setVisibility(View.GONE);
        }
        typedArray.recycle();
    }

    /**
     * 根据不同屏幕密度计算字体大小
     * 
     * Because... NORMAL is NOT hdpi... Normal is mdpi (160dpi) = 1.0x. hdpi
     * (240dpi) is 1.5x. xhdpi (320dpi) is 2.0x. xxdpi (480dpi) is 3.0x. xxxhdpi
     * (640dpi) is 4.0x. And (last, but not least) ldpi (120dpi) is 0.75x.
     */
    private void computeTextSize(int dpi) {
        switch (dpi) {
        case 120:
            mTvSize = (float) (mTvSize / 0.75);
            mRightSize = (float) (mRightSize / 0.75);
            break;
        case 240:
            mTvSize = (float) (mTvSize / 1.5);
            mRightSize = (float) (mRightSize / 1.5);
            break;
        case 320:
            mTvSize = mTvSize / 2;
            mRightSize = mRightSize / 2;
            break;
        case 480:
            mTvSize = mTvSize / 3;
            mRightSize = mRightSize / 3;
            break;
        case 640:
            mTvSize = mTvSize / 4;
            mRightSize = mRightSize / 4;
            break;
        default:
            break;
        }
    }

    /**
     * 获取屏幕参数:宽,高
     * 
     * @param context
     */
    private DisplayMetrics getScreenParams(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.img_left://左边按钮
            if (mTopViewLeftListener != null) {
                mTopViewLeftListener.onTopViewClick(v);
            }
            break;
        case R.id.layout://右边按钮
            if (mTopViewRightListener != null) {
                mTopViewRightListener.onTopViewClick(v);
            }
            break;
        }

    }

    /**
     * 定义一个接口
     */
    public interface TopViewListener {
        void onTopViewClick(View v);
    }

    /**
     * 自定义控件的点击事件
     */
    public void setonLeftClick(TopViewListener topViewListener) {
        mTopViewLeftListener = topViewListener;
    }

    public void setonRightClick(TopViewListener topViewListener) {
        mTopViewRightListener = topViewListener;
    }

}

attr.xml:定义控件属性

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="topView">
        <attr name="TopViewColor" format="color" />
        <attr name="TopViewSize" format="dimension" />
        <attr name="TopViewText" format="string" />
        <attr name="RightSize" format="dimension" />
        <attr name="RightText" format="string" />
        <attr name="LeftSrc" format="reference" />
        <attr name="RightSrc" format="reference" />
        <attr name="isRightShow" format="boolean" />
        <attr name="isLeftShow" format="boolean" />
    </declare-styleable>

</resources>

topview.xml:

<?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="wrap_content"
    android:background="@color/bg_color" >


    <ImageView
        android:id="@+id/img_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="15dp"
        android:src="@drawable/daidide_fanhui" />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="title"
        android:textColor="@color/white"
        android:textSize="20sp" />

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="15dp"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/img_right"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/daidideerj" />

        <TextView
            android:id="@+id/tv_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1111"
            android:textColor="#ffffff"
            android:textSize="12sp" />
    </LinearLayout>

</RelativeLayout>

引用控件:
activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:top="http://schemas.android.com/apk/res/com.example.topview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.topview.TopView
        android:id="@+id/topview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        top:LeftSrc="@drawable/daidide_fanhui"
        top:RightSize="12sp"
        top:RightSrc="@drawable/daidideerj"
        top:RightText="联系客服"
        top:TopViewColor="#ffffff"
        top:TopViewSize="20sp"
        top:isRightShow="false"
        top:TopViewText="标题" />

</LinearLayout>
public class MainActivity extends Activity {
    private TopView mTopView;
    private Activity mActivity = MainActivity.this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTopView = (TopView) findViewById(R.id.topview);
        mTopView.setonLeftClick(new TopViewListener() {
            @Override
            public void onTopViewClick(View v) {
                Toast.makeText(mActivity, "left", Toast.LENGTH_SHORT).show();
            }
        });
        mTopView.setonRightClick(new TopViewListener() {
            @Override
            public void onTopViewClick(View v) {
                Toast.makeText(mActivity, "right", Toast.LENGTH_SHORT).show();
            }
        });
    }

}

记录一下。
代码中关于字体大小适配的方法不是很好,希望大神看到可以告知好的方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值