import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class CustomTitleBar extends RelativeLayout {
private Button titleBarLeftBtn;
private Button titleBarRightBtn;
private TextView titleBarTitle;
public CustomTitleBar(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.custom_title_bar, this, true);
// View.inflate(context, R.layout.custom_title_bar, this);
titleBarLeftBtn = (Button) findViewById(R.id.title_bar_left);
titleBarRightBtn = (Button) findViewById(R.id.title_bar_right);
titleBarTitle = (TextView) findViewById(R.id.title_bar_title);
titleBarTitle.setText("32434234234");
// TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleBar);
// if (attributes != null) {
// //先处理左边按钮
// //获取是否要显示左边按钮
// boolean leftButtonVisible = attributes.getBoolean(R.styleable.CustomTitleBar_left_button_visible, true);
// if (leftButtonVisible) {
// titleBarLeftBtn.setVisibility(View.VISIBLE);
// } else {
// titleBarLeftBtn.setVisibility(View.INVISIBLE);
// }
// //设置左边按钮的文字
// String leftButtonText = attributes.getString(R.styleable.CustomTitleBar_left_button_text);
// if (!TextUtils.isEmpty(leftButtonText)) {
// titleBarLeftBtn.setText(leftButtonText);
// } else {
// //设置左边图片icon 这里是二选一 要么只能是文字 要么只能是图片
// int leftButtonDrawable = attributes.getResourceId(R.styleable.CustomTitleBar_left_button_drawable, R.mipmap.ic_launcher);
// if (leftButtonDrawable != -1) {
// titleBarLeftBtn.setBackgroundResource(leftButtonDrawable);
// }
// }
//
// //处理标题
// //先获取标题是否要显示图片icon
// int titleTextDrawable = attributes.getResourceId(R.styleable.CustomTitleBar_title_text_drawable, -1);
// if (titleTextDrawable != -1) {
// titleBarTitle.setBackgroundResource(titleTextDrawable);
// } else {
// //如果不是图片标题 则获取文字标题
// String titleText = attributes.getString(R.styleable.CustomTitleBar_title_text);
// if (!TextUtils.isEmpty(titleText)) {
// titleBarTitle.setText(titleText);
// }
// }
//
// //先处理右边按钮
// //获取是否要显示右边按钮
// boolean rightButtonVisible = attributes.getBoolean(R.styleable.CustomTitleBar_right_button_visible, true);
// if (rightButtonVisible) {
// titleBarRightBtn.setVisibility(View.VISIBLE);
// } else {
// titleBarRightBtn.setVisibility(View.INVISIBLE);
// }
// //设置右边按钮的文字
// String rightButtonText = attributes.getString(R.styleable.CustomTitleBar_right_button_text);
// if (!TextUtils.isEmpty(rightButtonText)) {
// titleBarRightBtn.setText(rightButtonText);
// //设置右边按钮文字颜色
// } else {
// //设置右边图片icon 这里是二选一 要么只能是文字 要么只能是图片
// int rightButtonDrawable = attributes.getResourceId(R.styleable.CustomTitleBar_right_button_drawable, -1);
// if (rightButtonDrawable != -1) {
// titleBarRightBtn.setBackgroundResource(rightButtonDrawable);
// }
// }
// attributes.recycle();
// }
}
public void setTitleClickListener(OnClickListener onClickListener) {
if (onClickListener != null) {
titleBarLeftBtn.setOnClickListener(onClickListener);
titleBarRightBtn.setOnClickListener(onClickListener);
}
}
public Button getTitleBarLeftBtn() {
return titleBarLeftBtn;
}
public Button getTitleBarRightBtn() {
return titleBarRightBtn;
}
public TextView getTitleBarTitle() {
return titleBarTitle;
}
}
//布局
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/title_bar_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:background="@null"
android:minHeight="45dp"
android:minWidth="45dp"
android:textSize="14sp" />
<TextView
android:id="@+id/title_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:singleLine="true"
android:textSize="17sp" />
<Button
android:id="@+id/title_bar_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="7dp"
android:background="@null"
android:minHeight="45dp"
android:minWidth="45dp"
android:textSize="14sp" />
</merge>
布局
<com.example.customtitlebar.CustomTitleBar
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="10dp"
lee:left_button_text="左边"
lee:right_button_drawable="@mipmap/ic_launcher"
lee:title_text="标题6"
lee:title_text_color="#000" />
自定义viewgroup控件-麦片
最新推荐文章于 2021-09-16 19:30:50 发布