标签:
<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name ="Topbar"><attr name = "title" format="string"/><attr name = "titleSize" 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>
public class Topbar extends RelativeLayout {private Button leftButton,rightButton;private TextView tvText;/*** 左边字体的颜色*/private int leftTextColor;/*** 左边背景的颜色*/private Drawable leftBackground;/*** 左边butto文字*/private String leftText;/*** 右边字体的颜色*/private int rightTextColor;/*** 右边背景的颜色*/private Drawable rightBackground;/*** 右边butto文字*/private String rightText;/*** title要显示文字的大小*/private float titleTextSize;/*** title字体的颜色*/private int titleTextColor;/*** title的文字*/private String title;private LayoutParams leftParams,rightParams,titleParams;private topbarClickListener listener;/*** 实现自己的接口* @author Kevin**/public interface topbarClickListener{public void leftClick();public void rightClick();}/*** 将传进来的映射* @param listener*/public void setOnTopClickListener(topbarClickListener listener){this.listener = listener;}//需要自定义属性,所以需要attrs参数public Topbar(Context context, AttributeSet attrs) {super(context, attrs);//存取自定义属性中的值返回到TypedArrayTypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.Topbar);//完成属性赋值leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);rightText = ta.getString(R.styleable.Topbar_rightText);rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);leftText = ta.getString(R.styleable.Topbar_leftText);titleTextSize = ta.getDimension(R.styleable.Topbar_titleSize,0);titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor,0);title = ta.getString(R.styleable.Topbar_title);//TypedArray一定要回收,避免浪费资源和由于缓存造成的错误ta.recycle();leftButton = new Button(context);rightButton = new Button(context);tvText = new TextView(context);//自定义属性赋值给自定义的控件leftButton.setTextColor(leftTextColor);leftButton.setBackgroundDrawable(leftBackground);leftButton.setText(leftText);rightButton.setTextColor(rightTextColor);rightButton.setBackgroundDrawable(rightBackground);rightButton.setText(rightText);tvText.setTextColor(titleTextColor);tvText.setTextSize(titleTextSize);tvText.setText(title);tvText.setGravity(Gravity.CENTER);setBackgroundColor(0xFFF59563);//设置布局参数leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);//居左对齐leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);//以leftParams的方式添加进去addView(leftButton,leftParams);//设置布局参数rightParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);//居右对齐rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);addView(rightButton,rightParams);titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);addView(tvText,titleParams);leftButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {listener.leftClick();}});rightButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {listener.rightClick();}});}}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:custom="http://schemas.android.com/apk/res/com.llc.defineview"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.llc.defineview.MainActivity" ><com.llc.defineview.Topbarandroid:id="@+id/topbar"android:layout_width="match_parent"android:layout_height="40dp"custom:leftBackground="@drawable/back_bg"custom:leftText="back"custom:leftTextColor="#ffffff"custom:rightBackground="@drawable/edit_bg"custom:rightText="more"custom:rightTextColor="#ffffff"custom:title="自定义标题"custom:titleTextColor="#123421"custom:titleSize="10sp"></com.llc.defineview.Topbar></RelativeLayout>
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Topbar topbar = (Topbar) findViewById(R.id.topbar);topbar.setOnTopClickListener(new Topbar.topbarClickListener() {@Overridepublic void rightClick() {Toast.makeText(MainActivity.this, "left", Toast.LENGTH_LONG).show();}@Overridepublic void leftClick() {Toast.makeText(MainActivity.this, "right", Toast.LENGTH_LONG).show();}});}}
标签:
原文地址:http://www.cnblogs.com/fruitbolgs/p/4251787.html