<declare-styleable name="titleBar"> <attr name="title" format="string" /> <attr name="titleTextSize" format="dimension" /> <attr name="titleTextColor" format="color" /> <attr name="titleLeftText" format="string" /> <attr name="titleLeftBackground" format="color|reference" /> <attr name="titleLeftTextColor" format="color" /> <attr name="titleRightText" format="string" /> <attr name="titleRightBackground" format="color|reference" /> <attr name="titleRightTextColor" format="color" /> </declare-styleable>
private void initAttrs(AttributeSet attrs) {
TypedArray ta = this.getContext().obtainStyledAttributes(attrs,
R.styleable.titleBar);
if (ta != null) {
title = ta.getString(R.styleable.titleBar_title);
titleTextSize = ta.getDimension(R.styleable.titleBar_titleTextSize,
16);
titleTextColor = ta
.getColor(R.styleable.titleBar_titleTextColor, 0);
titleLeftText = ta.getString(R.styleable.titleBar_titleLeftText);
titleLeftBackground = ta
.getDrawable(R.styleable.titleBar_titleLeftBackground);
titleLeftTextColor = ta.getColor(
R.styleable.titleBar_titleLeftTextColor, 0);
titleRightText = ta.getString(R.styleable.titleBar_titleRightText);
titleRightBackground = ta
.getDrawable(R.styleable.titleBar_titleRightBackground);
titleRightTextColor = ta.getColor(
R.styleable.titleBar_titleRightTextColor, 0);
ta.recycle();
}
}
private void initView() {
leftButton = new Button(getContext());
titleTextView = new TextView(getContext());
rightButton = new Button(getContext());
leftButton.setTextColor(titleLeftTextColor);
leftButton.setBackgroundDrawable(titleLeftBackground);
leftButton.setText(titleLeftText);
rightButton.setTextColor(titleRightTextColor);
rightButton.setBackgroundDrawable(titleRightBackground);
rightButton.setText(titleRightText);
titleTextView.setText(title);
titleTextView.setTextSize(titleTextSize);
titleTextView.setTextColor(titleTextColor);
mLeftLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT);
mLeftLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
addView(leftButton, mLeftLayoutParams);
mCenterLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT);
mCenterLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
addView(titleTextView, mCenterLayoutParams);
mRightLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT);
mRightLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
addView(rightButton, mRightLayoutParams);
}
public interface ClickListener {
void Click(int tag);
}
private ClickListener listener;
private void setListener() {
leftButton.setOnClickListener(this);
rightButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (listener != null) {
if (v == leftButton) {
listener.Click(LEFT_BUTTON);
} else if (v == rightButton) {
listener.Click(RIGHT_BUTTON);
}
}
}
titleBar.setListener(new ClickListener() {
@Override
public void Click(int tag) {
switch (tag) {
case TitleBar.LEFT_BUTTON:
Toast.makeText(MainActivity.this, "左边按钮被点击了", 0).show();
break;
case TitleBar.RIGHT_BUTTON:
Toast.makeText(MainActivity.this, "右边按钮被点击了", 0).show();
break;
default:
break;
}
}
});
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res/com.example.test" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp" tools:context=".MainActivity"> <!-- <include layout="@layout/topbar" /> --> <com.example.test.TitleBar android:id="@+id/titleBar" android:layout_width="match_parent" android:layout_height="40dp" custom:titleLeftBackground="@drawable/blue_button" custom:titleLeftText="Back" custom:titleLeftTextColor="#FFFFFF" custom:titleRightBackground="@drawable/blue_button" custom:titleRightText="More" custom:titleRightTextColor="#FFFFFF" custom:title="自定义标题" custom:titleTextColor="#123412" custom:titleTextSize="10sp"/> </RelativeLayout>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有