自定义ToolBar
来源:互联网 发布:神龙网络营销软件下载 编辑:程序博客网 时间:2024/05/02 05:24
自定义ToolBar,实现最大的自由度
一、自定义控件
/** * 自定义的标题 */public class TitleLayout extends RelativeLayout { private TextView tvTitle; private TextView tvRight; public TitleLayout(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.titlebar_bfmg, this); findViewById(R.id.tx_back).setOnClickListener(new OnClickListener() {//内部处理返回键 @Override public void onClick(View v) { ((Activity) getContext()).finish(); } }); tvTitle = (TextView) findViewById(R.id.tx_titleName); tvRight = (TextView) findViewById(R.id.tv_right); // 获得自定义属性 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TitleLayout); boolean isRightVisible = a.getBoolean(R.styleable.TitleLayout_isRightVisible, false); tvTitle.setText(a.getString(R.styleable.TitleLayout_titleString)); tvRight.setText(a.getString(R.styleable.TitleLayout_rightString)); if (isRightVisible) { tvRight.setVisibility(VISIBLE); } else { tvRight.setVisibility(INVISIBLE); } tvRight.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {//设置右边点击事件 if (rightOnClickListener != null) { rightOnClickListener.onClick(); } } }); } public interface RightOnClickListener { public void onClick(); } RightOnClickListener rightOnClickListener; public void setRightOnClickListener(RightOnClickListener rightOnClickListener) { this.rightOnClickListener = rightOnClickListener; }}二、布局
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/title" android:layout_width="match_parent" android:layout_height="44dp" android:background="@color/white"> <TextView android:id="@+id/tx_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:clickable="true" android:background="@drawable/back_btn_selector"/> <TextView android:id="@+id/tx_titleName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:text="收银台" android:textColor="#684E75" android:textSize="@dimen/top_bar_title_size"/> <TextView android:id="@+id/tv_right" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentRight="true" android:gravity="center" android:paddingLeft="12dp" android:paddingRight="@dimen/common_dp12" android:textColor="#684E75" android:visibility="invisible" android:clickable="true" /></RelativeLayout>三、属性(attr.xml)
<declare-styleable name="TitleLayout"> <attr name="titleString" format="string" /> <attr name="isRightVisible" format="boolean"/> <attr name="rightString" format="string"/></declare-styleable>
0 0
- 自定义toolbar
- 自定义toolbar
- 自定义ToolBar
- 自定义ToolBar
- 自定义ToolBar
- 自定义ToolBar
- 自定义Toolbar
- 自定义Toolbar
- 自定义toolbar
- 自定义toolbar
- 自定义toolbar
- 自定义ToolBar
- 自定义toolbar
- Adnroid Toolbar 自定义Toolbar布局
- jsf自定义toolbar组件
- jsf自定义toolbar组件
- jsf自定义toolbar组件
- jsf自定义toolbar组件
- Android App 沉浸式状态栏解决方案
- $_SERVER['PHP_SELF']、$_SERVER['SCRIPT_NAME'] 与 $_SERVER['REQUEST_URI'] 三者的区别
- Hadoop 2.0的两种RPC
- javaweb
- Git教程
- 自定义ToolBar
- 关于Centos7下搭建LNMP环境的问题小结
- linux shell 编程(四)
- 找工作 /*赶集网,58同城弱爆了 如何解决就业失业难题,赚取最多的钱*/ ------->>> 奶牛&FJ!!!
- C++ 对象的内存布局(上)
- 1040 最大公约数之和
- 如何让一个div在当前窗口垂直居中
- Codeforces Beta Round #95 (Div. 2) -- E. Yet Another Task with Queens(STL)
- 共轭分布