自定义顶部标题栏和其事件监听设置
来源:互联网 发布:银行数据安全管理办法 编辑:程序博客网 时间:2024/06/10 21:18
iOS系统上方的工具栏很漂亮,也很实用,下面让我们来仿制一下吧。
首先新建一个布局文件title.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/title_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5sp" android:background="@drawable/back_bg" android:text="Back" /> <TextView android:id="@+id/title_text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:text="Title Text" android:textSize="24sp" android:background="@drawable/edit_bg" /> <Button android:id="@+id/title_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5sp" android:text="Edit" android:background="@drawable/edit_bg"/></LinearLayout>
新建一个class--TitleLayout继承自LinearLayout,让它成为我们自定义的标题栏控件,代码如下:
package org.lxh.demo;import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.widget.LinearLayout;public class TitleLayout extends LinearLayout {public TitleLayout(Context context, AttributeSet attrs) {super(context, attrs);LayoutInflater.from(context).inflate(R.layout.title, this);}}
然后将我们自定义的控件加入到main.xml文件中:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@android:color/white"> <org.lxh.demo.TitleLayout android:layout_width="match_parent" android:layout_height="wrap_content" > </org.lxh.demo.TitleLayout></LinearLayout>
运行实例如下:
样子是实现了,下面我们尝试加入一些事件的监听操作,修改TitleLayout.java代码:
package org.lxh.demo;import android.app.Activity;import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;import android.widget.Toast;public class TitleLayout extends LinearLayout {private Context mContext;public TitleLayout(Context context, AttributeSet attrs) {super(context, attrs);mContext=context;LayoutInflater.from(context).inflate(R.layout.title, this);Button titleBackButton=(Button)findViewById(R.id.title_back);Button titleEdit=(Button)findViewById(R.id.title_edit);titleBackButton.setOnClickListener(new OnClickListener() {public void onClick(View v) {((Activity) mContext).finish();}});titleEdit.setOnClickListener(new OnClickListener() {public void onClick(View v) {Toast.makeText(mContext, "titleEdit 事件!", Toast.LENGTH_SHORT).show();}});}}
运行实例:
喜欢的朋友可以关注我!另我的应用下载地址:
http://openbox.mobilem.360.cn/index/d/sid/2989761
1 0
- 自定义顶部标题栏和其事件监听设置
- [Android] 自定义顶部标题栏
- 自定义的顶部标题栏
- 顶部标题栏自定义
- 自定义顶部标题栏
- 自定义dialog以及其监听事件
- 顶部标题栏(四)自定义ActionBar风格和样式
- 自定义顶部标题栏的view
- android实现自定义顶部标题栏
- 创建和监听自定义事件
- 如何给自定义listview设置监听事件
- Android Activity 标题栏 自定义 和 设置
- 实现自定义顶部标题栏(自定义控件)
- 标题栏设置及自定义标题栏
- JAVA 如何设置JTable的渲染器以及是否可编辑属性和其上的事件监听机制
- Android项目中自定义顶部标题栏
- 自定义View之简单顶部标题栏
- Android项目中自定义顶部标题栏
- 第12周-多继承、虚基类,继承和组合-课后实践·阅读程序
- pthread_cond_wait详解
- C++找对象的季节(补)——信春哥是否能永生?
- MVC简单生成静态化页面
- 软考之路--尾声
- 自定义顶部标题栏和其事件监听设置
- tarjan求强联通分量
- 经典的延续还是失败的开始?ARM cortex m7性能初探
- 程序员的量化交易之路(8)---金融概念Listing&delisting
- 99%的海量数据处理面试题
- Java开发环境搭建(JDK及其安装、运行第一个java程序)
- 使用线性布局实现如下界面的设计
- 黑暗世界统治下的光与暗-X11VNC配置
- 其实,你早知道答案