自定义TopBar
来源:互联网 发布:办公软件视频教程下载 编辑:程序博客网 时间:2024/06/16 07:48
1、分析自定义的TopBar所需要的属性,包括左按钮的字体颜色,字体内容,背景(leftTextColor,leftText,leftBackground),右按钮的字体颜色,字体内容,背景(rightTextColor,rightText,rigthtBackground),及标题的字体内容,字体颜色,字体大小(title,titleTextColor,titleTextSize)。在res目录下新建attrs文件,代码如下
<?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、实现一个我们自己的TopBar。可加入左右按钮点击事件,采用接口回调的方式,达到代码复用的目的。
package com.example.activity;import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.Gravity;import android.view.View;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 int leftTextColor;private Drawable leftBackground;private String leftText;private int rightTextColor;private Drawable rightBackground;private String rightText;private float titleTextSize;private int titleTextColor;private String title;private LayoutParams leftParams, rightParams, titleParams;private TopbarClickListener listener;public interface TopbarClickListener{public void leftClick();public void rightClick();}public void setOnTopClickListener(TopbarClickListener listener){this.listener = listener;}public TopBar(final Context context, AttributeSet attrs) {super(context, attrs);TypedArray tArray = context.obtainStyledAttributes(attrs,R.styleable.Topbar);leftTextColor = tArray.getColor(R.styleable.Topbar_leftTextColor, 0);leftBackground = tArray.getDrawable(R.styleable.Topbar_leftBackground);leftText = tArray.getString(R.styleable.Topbar_leftText);rightTextColor = tArray.getColor(R.styleable.Topbar_rightTextColor, 0);rightBackground = tArray.getDrawable(R.styleable.Topbar_rightBackground);rightText = tArray.getString(R.styleable.Topbar_rightText);titleTextSize = tArray.getDimension(R.styleable.Topbar_titleTextSize, 0);titleTextColor = tArray.getColor(R.styleable.Topbar_titleTextColor, 0);title = tArray.getString(R.styleable.Topbar_title);tArray.recycle();leftButton = new Button(context);rightButton = new Button(context);tvTitle = new TextView(context);leftButton.setTextColor(leftTextColor);leftButton.setBackground(leftBackground);leftButton.setText(leftText);rightButton.setTextColor(rightTextColor);rightButton.setBackground(rightBackground);rightButton.setText(rightText);tvTitle.setText(title);tvTitle.setTextSize(titleTextSize);tvTitle.setTextColor(titleTextColor);tvTitle.setGravity(Gravity.CENTER);setBackgroundColor(0xcccccccc);leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);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(tvTitle, titleParams);leftButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {listener.leftClick();}});rightButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {listener.rightClick();}});}}3、使用我们的topBar。
xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res/com.example.activity" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${packageName}.${activityClass}" > <com.example.activity.TopBar android:id="@+id/topBar" android:layout_width="match_parent" android:layout_height="40dp" custom:leftTextColor="#000000" custom:leftBackground="#123456" custom:leftText="Back" custom:rightBackground="#123456" custom:rightText="More" custom:rightTextColor="#000000" custom:title="标题" custom:titleTextColor="#123412" custom:titleTextSize="15sp"/></RelativeLayout
在Activity中引用。
TopBar topBar = (TopBar) findViewById(R.id.topBar);topBar.setOnTopClickListener(new TopbarClickListener() {@Overridepublic void rightClick() {// TODO Auto-generated method stubToast.makeText(TopBarActivity.this, "you clicked the rightBtn", 0).show();}@Overridepublic void leftClick() {// TODO Auto-generated method stubToast.makeText(TopBarActivity.this, "you clicked the leftBtn", 0).show();}});
0 0
- 自定义TopBar
- 自定义TopBar
- 自定义topbar
- 自定义topbar
- 自定义TopBar
- 自定义Topbar
- 自定义TopBar
- Android 自定义标题栏Topbar
- 自定义view:topBar
- 自定义控件学习Topbar
- Android 自定义View:TopBar
- Android自定义TopBar
- 自定义控件之TopBar
- Android 自定义标题栏TopBar
- 自定义组合控件TopBar
- 自定义View(TopBar)
- 自定义控件之-TopBar
- 自定义组合控件TopBar
- Linux系统Shutdown命令定时关机详解
- mfc中类中的成员变量的调用问题
- 欢迎使用CSDN-markdown编辑器
- MYSQL 操作详解
- awk 数组
- 自定义TopBar
- Linux操作系统 fedroa21安装步骤---linux安装(六)
- MFC中用OpenCV显示图像,并跟踪鼠标显示图像的坐标与像素
- asp.net 2.0 下的表单验证Cookieless属性
- 状态机--状态机8,关于战斗兵种的复合状态和动作融合技术
- MVC 数据验证
- Apache Spark MLlib学习笔记(三)MLlib统计指标之关联/抽样/汇总
- shellcode - Python vs Perl
- iOS 不通过textfield代理(与html5交互) 收起键盘