android 自定义通用标题
来源:互联网 发布:解密 知乎 编辑:程序博客网 时间:2024/05/29 07:47
项目中往往几个界面使用同一类型的标题布局,如果每次都重新定义,不仅增加工作量,如果出现错误,进行修改,也会要修改很多界面,并且容易遗漏,出错,比较好的方法是定义一个通用的布局,并且封装布局上按钮之类的点击事件。
先简单介绍一下这个布局,左侧为一个按钮,中间为搜索对话框,点击弹出对话框,并且设置软键盘enter图标为搜索图标,代码中捕捉搜索事件,右侧为一个FrameLayout布局,放了一个ImageView和一个TextView,根据功能选择其中一个进行显示
title_with_edittext.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/id_top" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/id_left" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:padding="12dp" android:layout_weight="1" android:src="@mipmap/ic_launcher" /> <EditText android:id="@+id/id_editTextSearch" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:padding="12dp" android:layout_weight="3" android:imeOptions="actionSearch" android:singleLine="true" android:inputType="text" android:drawableLeft="@mipmap/ic_launcher" android:drawablePadding="10dp" android:hint="@string/title" android:textColor="#000000" android:textSize="15sp" /> <FrameLayout android:id="@+id/id_right" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1"> <ImageView android:id="@+id/id_rightImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:padding="12dp" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/id_rightText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:padding="12dp" android:textColor="#000000" /> </FrameLayout></LinearLayout>
对应的自定义代码
/** * Created by Administrator on 2015/12/15 0015. * 包含搜索输入框的标题栏 */public class TitleWithEditText extends LinearLayout { EditText editTextSearch;//搜索框 ImageView imageViewLeft;//左侧图片 ImageView imageViewRight;//右侧图片 TextView textViewRight;//右侧文本 FrameLayout frameLayoutRight; private titleLayoutClick mListener; public TitleWithEditText(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.title_with_edittext, this); editTextSearch = (EditText) findViewById(R.id.id_editTextSearch); imageViewLeft = (ImageView) findViewById(R.id.id_left); imageViewRight = (ImageView) findViewById(R.id.id_rightImage); textViewRight = (TextView) findViewById(R.id.id_rightText); frameLayoutRight = (FrameLayout) findViewById(R.id.id_right); imageViewRight.setVisibility(View.INVISIBLE); textViewRight.setVisibility(View.INVISIBLE); imageViewLeft.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mListener != null) { mListener.leftViewClick(); } } }); frameLayoutRight.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mListener != null) { mListener.rightViewClick(); } } }); //设置弹出键盘输入完成图标为搜索,点击搜索事件 editTextSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) { if (mListener != null) { mListener.edittextDoSearch(); } return true; } return false; } }); } //设置右侧显示的控件,图片或文本,viewId 0 显示图标,viewId 1 显示文本 public void setRightShow(int viewId) { imageViewRight.setVisibility(View.INVISIBLE); textViewRight.setVisibility(View.INVISIBLE); if (viewId == 0) { imageViewRight.setVisibility(View.VISIBLE); } else if (viewId == 1) { textViewRight.setVisibility(View.VISIBLE); } } public void setTitleLayoutListener(titleLayoutClick titleLayoutListener) { mListener = titleLayoutListener; } //设置右边文本内容 public void setTextViewRightTitle(String title) { textViewRight.setText(title); } public interface titleLayoutClick { public void leftViewClick();//左图片点击 public void rightViewClick();//右边点击 public void edittextDoSearch();//搜索框搜索事件 }}
使用布局
<xxx.UI.CustomTitles.TitleWithEditText android:id="@+id/id_titleLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@mipmap/top"> </xxx.UI.CustomTitles.TitleWithEditText>
titleWithEditText = (TitleWithEditText) findViewById(R.id.id_titleLayout); titleWithEditText.setRightShow(0); TitleWithEditText.titleLayoutClick titleLayoutClick = new TitleWithEditText.titleLayoutClick() { @Override public void leftViewClick() { PackageManager pm = getPackageManager(); boolean hasACamera = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) || pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT) || Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD || Camera.getNumberOfCameras() > 0; if (!hasACamera) {//不存在照相机 T.showShort(getApplicationContext(), "没有找到照相设备"); } else { //打开扫描界面扫描条形码或二维码 Intent openCameraIntent = new Intent(MapActivity.this, CaptureActivity.class); startActivityForResult(openCameraIntent, 0); } } @Override public void rightViewClick() { } @Override public void edittextDoSearch() { T.showShort(MapActivity.this,"进行搜索"); } }; titleWithEditText.setTitleLayoutListener(titleLayoutClick);
finish
0 0
- android 自定义通用标题
- 自定义通用的标题条目
- Android自定义通用适配器
- Android自定义通用适配器
- android自定义标题
- android 自定义标题样式
- Android 自定义 标题
- Android 自定义标题
- android 自定义标题文件
- android自定义标题
- android自定义标题
- android自定义标题
- Android标题样式自定义
- Android:自定义窗口标题
- Android自定义标题TitleView
- Android 自定义ViewPager标题
- android自定义通用下拉刷新
- Android 自定义通用的loadingview
- 读取文件最后一行数据
- comparing app performance to maximize theoretical limits
- 模仿log4j的日志字符串格式化log(format,args)
- 等比例宽高不失真压缩
- Scala pair RDD 统计均值,方差等
- android 自定义通用标题
- Hadley Wickham:一个改变了R的人
- android---Afinal开源框架的使用
- NO JVM installation found
- Paint 分析
- web前端H5培训开发设计师好吗
- Xcode提示:your session has expired please log in
- 第四章 闭包
- 如何编写高效的Android代码