自定义view

1.创建 atts.xml 存放res/values/atts.xml

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

<resources>
    <declare-styleable name="Topbar">
        <attr name="title" format="string" />
        <attr name="titleTextSize" format="dimension" />
        <attr name="titleTextColor" format="color" />
        <attr name="leftTextColor" format="color" />
        <attr name="leftBackground" format="reference|color" />
        <attr name="leftText" format="string" />
        <attr name="rightTextColor" format="color" />
        <attr name="rightBackground" format="reference|color" />
        <attr name="rightText" format="string" />
    </declare-styleable>

</resources>

2.建立一个类 使其与属性类联系起来

package com.exampl.testtopbar;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class TopBar extends RelativeLayout {


private Button leftButton, rightButton;
private TextView tvTitle;


private String title;
private float titleTextSize;
private int titleTextColor;


private int leftTextColor;
private Drawable leftBackground;
private String leftText;


private int rightTextColor;
private Drawable rightBackground;
private String rightText;


private LayoutParams leftParams, rightParams, titleParams;
private topBarClickListener listener;


public interface topBarClickListener {
public void leftClick();


public void rightClick();
}


public void setOnTopbarClickListener(topBarClickListener listener) {
this.listener = listener;
}


@SuppressLint("NewApi")
public TopBar(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
TypedArray array = context.obtainStyledAttributes(attrs,
R.styleable.Topbar);
title = array.getString(R.styleable.Topbar_title);
titleTextSize = array.getDimension(R.styleable.Topbar_titleTextSize, 0);
titleTextColor = array.getColor(R.styleable.Topbar_titleTextColor, 0);


leftTextColor = array.getColor(R.styleable.Topbar_leftTextColor, 0);
leftBackground = array.getDrawable(R.styleable.Topbar_leftBackground);
leftText = array.getString(R.styleable.Topbar_leftText);


rightTextColor = array.getColor(R.styleable.Topbar_rightTextColor, 0);
rightBackground = array.getDrawable(R.styleable.Topbar_rightBackground);
rightText = array.getString(R.styleable.Topbar_rightText);


array.recycle();
leftButton = new Button(context);
rightButton = new Button(context);
tvTitle = new TextView(context);


leftButton.setBackground(leftBackground);
leftButton.setText(leftText);
leftButton.setTextColor(leftTextColor);


rightButton.setBackground(rightBackground);
rightButton.setText(rightText);
rightButton.setTextColor(rightTextColor);


tvTitle.setText(title);
tvTitle.setTextSize(titleTextSize);
tvTitle.setTextColor(titleTextColor);


setBackgroundColor(0xFFF59563);
leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
addView(leftButton, leftParams);


rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
addView(rightButton, rightParams);


titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
addView(tvTitle, titleParams);
leftButton.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
listener.leftClick();
}
});
rightButton.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
listener.rightClick();
}
});
}


}

3.在布局文件中对自定义view的引用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.exampl.testtopbar"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <com.exampl.testtopbar.TopBar
        android:id="@+id/topBar"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        custom:leftBackground="@drawable/ic_launcher"
        custom:leftText="左边"
        custom:leftTextColor="#00E3E3"
        custom:rightBackground="@drawable/ic_launcher"
        custom:rightText="右边"
        custom:rightTextColor="#00E3E3"
        custom:title="hello"
        custom:titleTextColor="#000000"
        custom:titleTextSize="18sp" >
    </com.exampl.testtopbar.TopBar>


</RelativeLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值