首页 > 其他分享 >自定义顶部标题栏和其事件监听设置

自定义顶部标题栏和其事件监听设置

时间:2022-11-11 13:03:25浏览次数:76  
标签:layout 自定义 title 标题栏 id content import android 监听


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();

}
});
}

}

运行实例:

自定义顶部标题栏和其事件监听设置_自定义控件_02

喜欢的朋友可以关注我!另我的应用下载地址:

​http://openbox.mobilem.360.cn/index/d/sid/2989761​

 

标签:layout,自定义,title,标题栏,id,content,import,android,监听
From: https://blog.51cto.com/u_15866446/5844731

相关文章

  • Android实战简易教程-第四十六枪(自定义控件体验之罗盘)
    前言作为一名有创新意思的开发人员,你迟早会发现内置的控件会满足不了你的想象力。拥有扩展已存在的视图、组建复合的控件以及创建独特的新视图能力,可以创建出最适合自己应用......
  • MapReduce实战之小文件处理案例(自定义InputFormat)
    小文件处理案例(自定义InputFormat)1)需求无论hdfs还是mapreduce,对于小文件都有损效率,实践中,又难免面临处理大量小文件的场景,此时,就需要有相应解决方案。将多个小文件合并成一......
  • 注解小结及自定义注解
    注解是Java开发中的一个高段位武器,我们可以在很多优秀的开源项目中看到注解的存在。比如,retrofit,eventbus。这些框架里面或多或少都用到了注解。注解使得项目使用起来非常......
  • Vue中如何自定义过滤器 ?
    过滤器可以格式化我们所需要的数据格式 ;自定义过滤器分为全局和局部过滤器:全局过滤器在main.js中使用Vue.direct4ive(过滤器名字,定义过滤器的具体行为函数);......
  • 【XAML】 WindowChrome 自定义窗体样式
    导读【XAML】WindowChrome的功能详解背景 WPF有两种主流的自定义Window窗体的方案,都各有缺点。方法一、缺点《WPF编程宝典》介绍了使用WindowStyle="None"和AllowsT......
  • threejs raycaster 鼠标事件的监听
    91行,传入scene.childern,那么就是监听整个场景中所有的childern其实也可传入一个特定的物体的孩子,这样就会监听这个物体上的鼠标事件......
  • 定义一个Java类,用来描述订单,属性自定义
    示例代码packagecom.powernode.oo;publicclassOrder{/***名称*/privateStringname;/***订单标识*/privateStrin......
  • servlet实操之监听器Listener
    监听器实现一个监听器的接口:1、编写一个监听器publicclassOnlineCountListenerimplementsHttpSessionListener{//创建session的监听@Overridepubli......
  • java 自定义注解
    packagecom.tedu.in;importjava.lang.reflect.Field;publicclassTest{publicstaticvoidmain(String[]args){Class<User>user=User.class;......
  • C语言 函数02 自定义函数的参数
    实际参数(实参):真实传给函数的参数,叫实参。实参可以是:常量、变量、表达式、函数等。无论实参是何种类型的量,在进行函数调用时,它们都必须有确定的值,以便把这些值传送给形参。形......