自定义TopBar
来源:互联网 发布:java中遍历数组 编辑:程序博客网 时间:2024/06/05 15:24
近期在尝试一款RF手持终端的开发任务,由于系统版本要求,打算采用自定义topbar来作为导航栏,当然v7包里有更好的toolbar可供使用,这里简单做一下示例,仅供参考。
上代码:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorPrimary" > <LinearLayout android:id="@+id/ll_back" android:layout_width="40dp" android:layout_height="?attr/actionBarSize" android:gravity="center" > <ImageView android:id="@+id/iv_back" android:layout_width="25dp" android:layout_height="25dp" android:src="@drawable/left_back" /> </LinearLayout> <TextView android:id="@+id/tv_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/ll_back" android:layout_centerVertical="true" android:maxLength="4" android:textColor="@color/background_light" android:textSize="16sp" /> <TextView android:id="@+id/tv_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:maxLength="6" android:textColor="@color/background_light" android:textSize="16sp" /> <TextView android:id="@+id/tv_rightSecond" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/tv_rightMost" android:layout_marginRight="10dp" android:maxLength="6" android:textColor="@color/background_light" android:textSize="16sp" /> <TextView android:id="@+id/tv_rightMost" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:maxLength="5" android:textColor="@color/background_light" android:layout_marginRight="10dp" android:layout_alignParentRight="true" android:textSize="16sp" /></RelativeLayout>
public class CustomTitle extends RelativeLayout { private LayoutInflater inflater; private View mView; private TextView tv_left; private TextView tv_center; private TextView tv_rightMost; private TextView tv_rightSecond; private LinearLayout ll_back; private ImageView iv_back; private CustomTitle.CustomToolbarListener listener; public CustomTitle(Context context) { super(context); } public CustomTitle(Context context, AttributeSet attrs) { super(context, attrs); init(); initListener(); } public CustomTitle(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } private void init() { if (mView == null) { inflater = LayoutInflater.from(getContext()); mView = inflater.inflate(R.layout.toolbar_custom, null); tv_left = (TextView) mView.findViewById(R.id.tv_left); tv_center = (TextView) mView.findViewById(R.id.tv_center); tv_rightMost = (TextView) mView.findViewById(R.id.tv_rightMost); tv_rightSecond = (TextView) mView.findViewById(R.id.tv_rightSecond); ll_back = (LinearLayout) mView.findViewById(R.id.ll_back); iv_back = (ImageView) mView.findViewById(R.id.iv_back); //使用LayoutParams把控件添加到子view中 LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT); addView(mView, lp); } } private void initListener() { ll_back.setOnClickListener(v -> listener.back(null)); } public void setBackListener(CustomTitle.CustomToolbarListener backListener) { this.listener = backListener; } public void setTitle(CharSequence title) { if (tv_center != null) { if (!TextUtils.isEmpty(title)) { tv_center.setVisibility(VISIBLE); tv_center.setText(title); } else { tv_center.setVisibility(GONE); } } } public void setTvLeft(String tvLeft) { if (!TextUtils.isEmpty(tvLeft)) { tv_left.setVisibility(VISIBLE); tv_left.setText(tvLeft); } else { tv_left.setVisibility(GONE); } } public void setTvRightMost(String tvRightMost) { if (!TextUtils.isEmpty(tvRightMost)) { tv_rightMost.setVisibility(VISIBLE); tv_rightMost.setText(tvRightMost); } else { tv_rightMost.setVisibility(GONE); } } public void setTvRightSecond(String tvRightSecond) { if (!TextUtils.isEmpty(tvRightSecond)) { tv_rightSecond.setVisibility(VISIBLE); tv_rightSecond.setText(tvRightSecond); } else { tv_rightSecond.setVisibility(GONE); } } public interface CustomToolbarListener<T> { void back(T t); }}
阅读全文
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
- Keras指定使用GPU
- postgresql~*符号的含义
- [C++模板技术] 线性继承体系自动生成
- FZU 2231 平行四边形数 (组合计数)
- 开学第一天学习练习
- 自定义TopBar
- mysql基础(二)
- mybatis入门之Helloworld
- 实现图片的拖拽缩放功能
- Sublime Text编辑器如何显示顶部的菜单栏
- Android开发对网络缓存的理解
- 2.4g无线跳频(三)
- Problem 2150 Fire Game
- 栈的C++实现